<?php
namespace AICSAPP;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Admin {

    public static function init() {
        add_action( 'admin_menu', [ __CLASS__, 'menu' ] );
        add_action( 'admin_enqueue_scripts', [ __CLASS__, 'assets' ] );
    }

    public static function menu() {
        add_menu_page(
            'AISaleBot',
            'AISaleBot',
            'manage_options',
            'aicsapp-chatbot',
            [ __CLASS__, 'render' ],
            'dashicons-robot',
            57
        );
    }

    public static function assets( $hook ) {
        if ( $hook !== 'toplevel_page_aicsapp-chatbot' ) {
            return;
        }

        wp_enqueue_style(
            'aicsapp-admin',
            AICSAPP_URL . 'assets/admin.css',
            [],
            AICSAPP_VER
        );

        wp_enqueue_script(
            'aicsapp-admin',
            AICSAPP_URL . 'assets/admin.js',
            [ 'jquery' ],
            AICSAPP_VER,
            true
        );
    }

    public static function render() {
        self::handle_actions();
        ?>
        <div class="wrap aicsapp-admin">

            <!-- Header -->
            <div class="aicsapp-header">
                <img
                    src="<?php echo esc_url( AICSAPP_URL . 'assets/logo.png' ); ?>"
                    alt="AISaleBot"
                    class="aicsapp-logo"
                />
                <div class="aicsapp-brand-text">
                    <div class="aicsapp-brand-name">AISaleBot</div>
                    <div class="aicsapp-brand-sub">
                        <?php esc_html_e( 'Powered by AI. Driven by Results.', 'aisalebot' ); ?>
                    </div>
                </div>
            </div>

            <!-- Tabs -->
            <nav class="nav-tab-wrapper">
                <a href="#tutorial" class="nav-tab nav-tab-active">
                    <?php esc_html_e( 'Installation Guide', 'aisalebot' ); ?>
                </a>
                <a href="#integration" class="nav-tab">
                    <?php esc_html_e( 'Integration', 'aisalebot' ); ?>
                </a>
            </nav>

            <!-- Tutorial -->
            <div id="tutorial" class="aicsapp-tab active">
                <h2>Getting Started</h2>

                <div class="aicsapp-tutorial">

                    <!-- STEP 1 -->
                    <div class="aicsapp-step">
                        <div class="aicsapp-step-number">1</div>

                        <div class="aicsapp-step-content">
                            <strong>Create an AISaleBot account</strong>
                            <p>
                                Sign up to access the AISaleBot dashboard, where you can manage
                                chatbots, data sources, and appearance settings.
                            </p>
                            <p>
                                <a href="https://aisalebot.io/signup?platform=wordpress"
                                target="_blank"
                                class="button button-primary">
                                    Sign up for AISaleBot
                                </a>
                            </p>
                        </div>

                        <div class="aicsapp-step-media">
                            <img src="<?php echo esc_url( AICSAPP_URL . 'assets/Tutorial1.png' ); ?>" alt="tutorial">
                        </div>
                    </div>

                    <!-- STEP 2 -->
                    <div class="aicsapp-step">
                        <div class="aicsapp-step-number">2</div>
                        <div class="aicsapp-step-content">
                            <strong>Create and train your chatbot</strong>
                            <p>
                                Create a new chatbot in the AISaleBot dashboard.
                                You can train it by entering your website URL to crawl product data,
                                or by uploading a product data file (CSV or supported formats).
                            </p>
                            <p>
                                You can also customize the chatbot’s appearance, colors, and behavior
                                before publishing.
                            </p>
                        </div>
                        <div class="aicsapp-step-media">
                            <img src="<?php echo esc_url( AICSAPP_URL . 'assets/Tutorial2.png' ); ?>" alt="tutorial">
                        </div>
                    </div>

                    <!-- STEP 3 -->
                    <div class="aicsapp-step">
                        <div class="aicsapp-step-number">3</div>
                        <div class="aicsapp-step-content">
                            <strong>Copy your Chatbot ID</strong>
                            <p>
                                After the system finishes setting up your chatbot, AISaleBot will generate
                                a unique <b>Chatbot ID</b> for your website.
                            </p>
                            <p>
                                In the AISaleBot dashboard, locate the embed script provided for your chatbot
                                and copy only the value of the <code>data-id</code> attribute.
                            </p>
                            <p>
                                The Chatbot ID will look similar to the example shown below.
                            </p>
                        </div>
                        <div class="aicsapp-step-media">
                            <img src="<?php echo esc_url( AICSAPP_URL . 'assets/Tutorial3.png' ); ?>" alt="tutorial">
                        </div>
                    </div>

                    <!-- STEP 4 -->
                    <div class="aicsapp-step">
                        <div class="aicsapp-step-number">4</div>
                        <div class="aicsapp-step-content">
                            <strong>Paste the Chatbot ID</strong>
                            <p>
                                After creating your chatbot, copy the Chatbot ID provided by AISaleBot.
                                Paste it into the <b>Integration</b> tab in WordPress and click <b>Save</b>.
                            </p>
                            <p>
                                Once saved, the plugin will automatically load the AISaleBot widget
                                on your website.
                            </p>
                        </div>
                        <div class="aicsapp-step-media">
                            <img src="<?php echo esc_url( AICSAPP_URL . 'assets/Tutorial4.png' ); ?>" alt="tutorial">
                        </div>
                    </div>

                </div>
            </div>

            <!-- Integration -->
            <div id="integration" class="aicsapp-tab">
                <h2><?php esc_html_e( 'Integration', 'aisalebot' ); ?></h2>

                <form method="post">
                    <?php wp_nonce_field( 'aicsapp_save_settings' ); ?>

                    <table class="form-table" role="presentation">

                        <tr>
                            <th scope="row">
                                <?php esc_html_e( 'Chatbot ID', 'aisalebot' ); ?>
                            </th>
                            <td>
                                <input
                                    type="text"
                                    name="aicsapp_chatbot_id"
                                    value="<?php echo esc_attr( get_option( 'aicsapp_chatbot_id' ) ); ?>"
                                    class="regular-text"
                                    placeholder="eJwFwdEKgjAUANAvCray..."
                                />
                                <p class="description">
                                    <?php esc_html_e(
                                        'Enter the Chatbot ID provided by the AISaleBot dashboard.',
                                        'aisalebot'
                                    ); ?>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <?php esc_html_e( 'Enable Chatbot', 'aisalebot' ); ?>
                            </th>
                            <td>
                                <label>
                                    <input
                                        type="checkbox"
                                        name="aicsapp_enabled"
                                        value="1"
                                        <?php checked( get_option( 'aicsapp_enabled' ), 1 ); ?>
                                    />
                                    <?php esc_html_e( 'Load AISaleBot on frontend', 'aisalebot' ); ?>
                                </label>
                                <p class="description">
                                    <?php esc_html_e(
                                        'Enable or disable the AISaleBot widget on your website.',
                                        'aisalebot'
                                    ); ?>
                                </p>
                            </td>
                        </tr>
                    </table>
                    <p>
                        <button
                            type="submit"
                            name="save_settings"
                            class="button button-primary">
                            <?php esc_html_e( 'Save Settings', 'aisalebot' ); ?>
                        </button>
                    </p>
                </form>

                <hr />

                <form method="post">
                    <?php wp_nonce_field( 'aicsapp_export' ); ?>
                    <button
                        type="submit"
                        name="export_products"
                        class="button">
                        <?php esc_html_e( 'Export Products (CSV)', 'aisalebot' ); ?>
                    </button>
                </form>
            </div>

        </div>
        <?php
    }

    private static function handle_actions() {

        // Save settings (Enable + Chatbot ID)
        if (
            isset( $_POST['save_settings'] ) &&
            check_admin_referer( 'aicsapp_save_settings' )
        ) {

            // Enable / Disable chatbot
            $enabled = isset( $_POST['aicsapp_enabled'] ) ? 1 : 0;
            update_option( 'aicsapp_enabled', $enabled, false );

            // Save Chatbot ID
            if ( isset( $_POST['aicsapp_chatbot_id'] ) ) {
                update_option(
                    'aicsapp_chatbot_id',
                    sanitize_text_field( wp_unslash( $_POST['aicsapp_chatbot_id'] ) ),
                    false
                );
            }

            echo '<div class="updated notice"><p>' .
                esc_html__( 'Settings saved successfully.', 'aisalebot' ) .
            '</p></div>';
        }

        // Export products
        if (
            isset( $_POST['export_products'] ) &&
            check_admin_referer( 'aicsapp_export' )
        ) {
            if ( class_exists( '\AICSAPP\Data' ) ) {
                \AICSAPP\Data::export_products_csv();
            }
        }
    }
}
