<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Admin UI for layout-safe content replacement for a single post/page.
 *
 * @since 2.1.0
 */
class AI_Magic_Layout_Admin
{

    public function __construct()
    {
        // Menu registration disabled - use Theme Wizard instead
        // add_action('admin_menu', array($this, 'register_menu'), 20);
        add_action('admin_post_ai_magic_layout_update', array($this, 'handle_form'));
    }

    /**
     * Register submenu page.
     */
    public function register_menu()
    {

        add_submenu_page(
            'ai-magic-settings',
            __('Layout Content AI', 'ai-magic-settings'),
            __('Layout Content AI', 'ai-magic-settings'),
            'edit_posts',
            'ai-magic-layout',
            array($this, 'render_page')
        );
    }

    /**
     * Render admin page with simple form.
     */
    public function render_page()
    {

        if (!current_user_can('edit_posts')) {
            wp_die(__('You do not have permission to access this page.', 'ai-magic-settings'));
        }

        $updated = !empty($_GET['updated']);
        $post_id = isset($_GET['post_id']) ? absint($_GET['post_id']) : 0;

        ?>
        <div class="wrap">
            <h1><?php esc_html_e('Layout Content AI', 'ai-magic-settings'); ?></h1>

            <?php if ($updated): ?>
                <div id="message" class="updated notice notice-success is-dismissible">
                    <p><?php esc_html_e('Content updated successfully.', 'ai-magic-settings'); ?></p>
                </div>
            <?php endif; ?>

            <p><?php esc_html_e('Replace demo content (lorem ipsum) on a selected page using AI, without breaking the page builder layout.', 'ai-magic-settings'); ?>
            </p>

            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
                <?php wp_nonce_field('ai_magic_layout_update', 'ai_magic_layout_nonce'); ?>
                <input type="hidden" name="action" value="ai_magic_layout_update" />

                <table class="form-table" role="presentation">
                    <tbody>
                        <tr>
                            <th scope="row">
                                <label
                                    for="ai-magic-layout-post-id"><?php esc_html_e('Page/Post ID', 'ai-magic-settings'); ?></label>
                            </th>
                            <td>
                                <input type="number" class="regular-text" id="ai-magic-layout-post-id" name="post_id"
                                    value="<?php echo esc_attr($post_id); ?>" required />
                                <p class="description">
                                    <?php esc_html_e('Enter the ID of the page or post that uses a template (Elementor, Gutenberg, WPBakery, etc.).', 'ai-magic-settings'); ?>
                                </p>
                            </td>
                        </tr>

                        <tr>
                            <th scope="row">
                                <label
                                    for="ai-magic-layout-prompt"><?php esc_html_e('Company / Project Information', 'ai-magic-settings'); ?></label>
                            </th>
                            <td>
                                <textarea id="ai-magic-layout-prompt" name="prompt" rows="6" class="large-text"
                                    required></textarea>
                                <p class="description">
                                    <?php esc_html_e('Describe the company (name, sector, services, tone, language, location, etc.). This will be used to replace lorem ipsum texts.', 'ai-magic-settings'); ?>
                                </p>
                            </td>
                        </tr>
                    </tbody>
                </table>

                <?php submit_button(__('Update Content with AI', 'ai-magic-settings')); ?>
            </form>
        </div>
        <?php
    }

    /**
     * Handle form submission.
     */
    public function handle_form()
    {

        if (!current_user_can('edit_posts')) {
            wp_die(__('You do not have permission to perform this action.', 'ai-magic-settings'));
        }

        check_admin_referer('ai_magic_layout_update', 'ai_magic_layout_nonce');

        $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0;
        $prompt = isset($_POST['prompt']) ? sanitize_textarea_field($_POST['prompt']) : '';

        if ($post_id && $prompt && function_exists('ai_magic_layout_update_post')) {
            ai_magic_layout_update_post($post_id, $prompt);
        }

        $redirect = add_query_arg(
            array(
                'page' => 'ai-magic-layout',
                'post_id' => $post_id,
                'updated' => 1,
            ),
            admin_url('admin.php')
        );

        wp_safe_redirect($redirect);
        exit;
    }

}
