<?php
/**
 * Admin menu, settings registration, and SaaS dashboard.
 *
 * @package EyewearVirtualTryOn
 */

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

/**
 * Admin menu — top-level Tryonixs dashboard.
 *
 * @return void
 */
function eyewvitr_settings_menu() {
    add_menu_page(
        __('Tryonixs Virtual Try-On', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        __('Tryonixs', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'manage_options',
        'eyewvitr-settings',
        'eyewvitr_settings_page',
        'dashicons-visibility',
        56
    );

    add_submenu_page(
        'eyewvitr-settings',
        __('Dashboard', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        __('Dashboard', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'manage_options',
        'eyewvitr-settings',
        'eyewvitr_settings_page'
    );

    add_submenu_page(
        'eyewvitr-settings',
        __('Welcome', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        __('Setup Guide', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'manage_options',
        'eyewvitr-welcome',
        'eyewvitr_welcome_page'
    );
}
add_action('admin_menu', 'eyewvitr_settings_menu');

/**
 * Register plugin settings.
 *
 * @return void
 */
function eyewvitr_register_settings() {
    register_setting('eyewvitr_settings_group', 'eyewvitr_api_key', [
        'type'              => 'string',
        'sanitize_callback' => 'sanitize_text_field',
        'default'           => '',
    ]);

    register_setting('eyewvitr_settings_group', 'eyewvitr_app_url', [
        'type'              => 'string',
        'sanitize_callback' => 'esc_url_raw',
        'default'           => 'https://tryonixs.com',
    ]);

    register_setting('eyewvitr_settings_group', 'eyewvitr_sdk_url', [
        'type'              => 'string',
        'sanitize_callback' => 'esc_url_raw',
        'default'           => 'https://tryonixs.com/tryonixs-sdk.js',
    ]);

    register_setting('eyewvitr_settings_group', 'eyewvitr_button_text', [
        'type'              => 'string',
        'sanitize_callback' => 'sanitize_text_field',
        'default'           => 'Try Virtual Try-On',
    ]);

    register_setting('eyewvitr_settings_group', 'eyewvitr_button_position', [
        'type'              => 'string',
        'sanitize_callback' => 'eyewvitr_sanitize_button_position',
        'default'           => 'before_add_to_cart',
    ]);

    register_setting('eyewvitr_settings_group', 'eyewvitr_show_on_shop', [
        'type'              => 'string',
        'sanitize_callback' => 'eyewvitr_sanitize_yes_no',
        'default'           => 'yes',
    ]);
}
add_action('admin_init', 'eyewvitr_register_settings');

/**
 * Enqueue admin assets on plugin screens.
 *
 * @param string $hook Current admin page hook.
 * @return void
 */
function eyewvitr_admin_assets($hook) {
    // Match plugin screens even when menu title localization changes the hook prefix.
    if (strpos($hook, 'eyewvitr-settings') === false && strpos($hook, 'eyewvitr-welcome') === false) {
        return;
    }

    wp_enqueue_style(
        'eyewvitr-admin',
        EYEWVITR_PLUGIN_URL . 'assets/admin/admin.css',
        [],
        EYEWVITR_VERSION
    );

    wp_enqueue_script(
        'eyewvitr-admin',
        EYEWVITR_PLUGIN_URL . 'assets/admin/admin.js',
        [],
        EYEWVITR_VERSION,
        true
    );
}
add_action('admin_enqueue_scripts', 'eyewvitr_admin_assets');

/**
 * SaaS-style dashboard page.
 *
 * @return void
 */
function eyewvitr_settings_page() {
    if (!current_user_can('manage_options')) {
        return;
    }

    $settings = eyewvitr_get_settings();
    $status   = eyewvitr_get_setup_status();
    $connected = eyewvitr_is_connected();
    $done      = count(array_filter($status));

    settings_errors();
    ?>
    <div class="eyewvitr-admin wrap">
        <div class="eyewvitr-hero">
            <div class="eyewvitr-hero__brand">
                <div class="eyewvitr-logo" aria-hidden="true">T</div>
                <div>
                    <p class="eyewvitr-eyebrow"><?php esc_html_e('Tryonixs', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                    <h1><?php esc_html_e('Welcome to Tryonixs Virtual Try-On', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h1>
                    <p class="eyewvitr-hero__sub"><?php esc_html_e('Transform your WooCommerce store with AI-powered eyewear experiences.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                </div>
            </div>
            <a class="eyewvitr-btn eyewvitr-btn--ghost" href="https://tryonixs.com" target="_blank" rel="noopener noreferrer">
                <?php esc_html_e('Open Tryonixs', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
            </a>
        </div>

        <div class="eyewvitr-grid">
            <div class="eyewvitr-card eyewvitr-card--progress">
                <div class="eyewvitr-card__head">
                    <h2><?php esc_html_e('Setup Progress', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h2>
                    <span class="eyewvitr-badge"><?php echo esc_html(sprintf(/* translators: 1: completed steps, 2: total steps */ __('%1$d of %2$d complete', 'tryonixs-virtual-try-on-for-eyewear-stores'), $done, 4)); ?></span>
                </div>
                <ol class="eyewvitr-steps">
                    <li class="<?php echo $status['account'] ? 'is-done' : ''; ?>">
                        <span class="eyewvitr-step-dot"></span>
                        <?php esc_html_e('Create Account', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </li>
                    <li class="<?php echo $status['connected'] ? 'is-done' : ''; ?>">
                        <span class="eyewvitr-step-dot"></span>
                        <?php esc_html_e('Connect API Key', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </li>
                    <li class="<?php echo $status['products'] ? 'is-done' : ''; ?>">
                        <span class="eyewvitr-step-dot"></span>
                        <?php esc_html_e('Enable Products', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </li>
                    <li class="<?php echo $status['launched'] ? 'is-done' : ''; ?>">
                        <span class="eyewvitr-step-dot"></span>
                        <?php esc_html_e('Launch Try-On', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </li>
                </ol>
            </div>

            <div class="eyewvitr-card">
                <div class="eyewvitr-card__head">
                    <h2><?php esc_html_e('Connect Your Tryonixs Account', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h2>
                    <?php if ($connected) : ?>
                        <span class="eyewvitr-status eyewvitr-status--ok"><?php esc_html_e('Store Connected', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></span>
                    <?php else : ?>
                        <span class="eyewvitr-status"><?php esc_html_e('Connect Your Account', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></span>
                    <?php endif; ?>
                </div>

                <form method="post" action="options.php" class="eyewvitr-form">
                    <?php settings_fields('eyewvitr_settings_group'); ?>

                    <label class="eyewvitr-label" for="eyewvitr_api_key"><?php esc_html_e('Public API Key', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></label>
                    <input
                        id="eyewvitr_api_key"
                        type="text"
                        class="eyewvitr-input"
                        name="eyewvitr_api_key"
                        value="<?php echo esc_attr($settings['api_key']); ?>"
                        placeholder="pk_live____________"
                        autocomplete="off"
                    >

                    <div class="eyewvitr-actions">
                        <button type="submit" class="eyewvitr-btn eyewvitr-btn--primary">
                            <?php echo $connected ? esc_html__('Update Connection', 'tryonixs-virtual-try-on-for-eyewear-stores') : esc_html__('Connect Store', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                        </button>
                        <a class="eyewvitr-link" href="https://tryonixs.com" target="_blank" rel="noopener noreferrer">
                            <?php esc_html_e('Get your API key →', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                        </a>
                    </div>

                    <details class="eyewvitr-advanced">
                        <summary><?php esc_html_e('Display & advanced settings', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></summary>

                        <div class="eyewvitr-fields">
                            <div>
                                <label class="eyewvitr-label" for="eyewvitr_button_text"><?php esc_html_e('Button Text', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></label>
                                <input id="eyewvitr_button_text" type="text" class="eyewvitr-input" name="eyewvitr_button_text" value="<?php echo esc_attr($settings['button_text']); ?>">
                            </div>
                            <div>
                                <label class="eyewvitr-label" for="eyewvitr_button_position"><?php esc_html_e('Button Position', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></label>
                                <select id="eyewvitr_button_position" class="eyewvitr-input" name="eyewvitr_button_position">
                                    <option value="before_add_to_cart" <?php selected($settings['button_position'], 'before_add_to_cart'); ?>><?php esc_html_e('Before Add to Cart', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></option>
                                    <option value="after_add_to_cart" <?php selected($settings['button_position'], 'after_add_to_cart'); ?>><?php esc_html_e('After Add to Cart', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></option>
                                </select>
                            </div>
                            <div>
                                <label class="eyewvitr-label" for="eyewvitr_app_url"><?php esc_html_e('App URL', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></label>
                                <input id="eyewvitr_app_url" type="url" class="eyewvitr-input" name="eyewvitr_app_url" value="<?php echo esc_attr($settings['app_url']); ?>">
                            </div>
                            <div>
                                <label class="eyewvitr-label" for="eyewvitr_sdk_url"><?php esc_html_e('SDK Script URL', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></label>
                                <input id="eyewvitr_sdk_url" type="url" class="eyewvitr-input" name="eyewvitr_sdk_url" value="<?php echo esc_attr($settings['sdk_url']); ?>">
                            </div>
                        </div>

                        <input type="hidden" name="eyewvitr_show_on_shop" value="no">
                        <label class="eyewvitr-check">
                            <input type="checkbox" name="eyewvitr_show_on_shop" value="yes" <?php checked($settings['show_on_shop']); ?>>
                            <?php esc_html_e('Show try-on button on shop and category pages', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                        </label>
                    </details>
                </form>

                <?php if (!eyewvitr_is_woocommerce_active()) : ?>
                    <p class="eyewvitr-warning"><?php esc_html_e('WooCommerce is not active. Install WooCommerce to show try-on buttons on product pages.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                <?php endif; ?>
            </div>
        </div>

        <div class="eyewvitr-card eyewvitr-card--tips">
            <h2><?php esc_html_e('Launch checklist', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h2>
            <ul>
                <li><?php esc_html_e('Publish eyewear products with a clear featured image or transparent frame PNG.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></li>
                <li><?php esc_html_e('Keep Virtual Try-On enabled in the product editor (on by default).', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></li>
                <li><?php esc_html_e('Use shortcode [eyewvitr_button id="123"] for page builders.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></li>
            </ul>
        </div>
    </div>
    <?php
}
