<?php
/**
 * Contentosapp Studio Setup Wizard View
 *
 * @package Contentosapp_Studio
 * @since   3.0.0
 */

defined('ABSPATH') || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Included admin view; local presentation variables are scoped to this template.

// Support both module mode (ContentOS Core active) and standalone mode.
$settings   = costudio_settings();
$sites_repo = costudio_sites();
$license    = Costudio_Standalone_License::get_instance();

// ContentOS Auto is available as a managed service with server-side quota.
// Default to 'contentos' for first-time setups; respect saved setting otherwise.
$current_api_key = $settings ? $settings->get('ai_api_key', '') : '';
$is_key_set      = ! empty($current_api_key);
$stored          = $settings ? $settings->get('ai_provider', '') : '';
$ai_mode_saved = $settings ? $settings->get('ai_mode', '') : '';
if ( empty($stored) || ('gemini' === $stored && 'byok' !== $ai_mode_saved && ! $is_key_set) ) {
    // Fresh install or no explicit BYOK choice: default to ContentOS Auto.
    $current_provider = 'contentos';
} else {
    $current_provider = $stored;
}

$connected_sites = $sites_repo ? $sites_repo->get_all() : [];
$has_sites       = ! empty($connected_sites);
$operable_sites  = array_filter(
	$connected_sites,
	function( $site, $key ) use ( $sites_repo ) {
		return $sites_repo ? $sites_repo->is_site_operable( (string) $key ) : ! empty( $site['active'] );
	},
	ARRAY_FILTER_USE_BOTH
);

$max_sites   = PHP_INT_MAX;
$sites_count = count($connected_sites);
$can_add_site = $sites_count < $max_sites;

// Variant for Step 3: ContentOS Auto mode counts as configured (no API key needed)
$ai_mode = $settings ? $settings->get('ai_mode', '') : '';
$setup_complete = ($is_key_set || $ai_mode === 'contentos') && $has_sites;

$nonce = wp_create_nonce(Costudio_Admin::NONCE);

// Telemetry consent state — controls Step 3 CTA gating.
$telemetry_decided = class_exists('Costudio_Telemetry') && Costudio_Telemetry::has_user_decided();

// Mask API key for placeholder
$masked_key = $is_key_set ? '••••••••••••••••' . substr($current_api_key, -4) : '';

?>

<div class="wrap fos-wizard-wrap">
    
    <!-- Brand header -->
    <div class="fos-wizard-brand">
        <div class="fos-wizard-brand__left">
            <div class="fos-wizard-brand__icon">🏭</div>
            <div>
                <div class="fos-wizard-brand__title">Contentosapp Studio</div>
                <div class="fos-wizard-brand__sub"><?php esc_html_e('Production pipeline with 7 AI agents', 'contentosapp-studio'); ?></div>
            </div>
        </div>
        <a class="fos-wizard-brand__skip" href="<?php echo esc_url( admin_url('admin.php?page=costudio') ); ?>">
            <?php esc_html_e('Skip setup — go to dashboard', 'contentosapp-studio'); ?>
        </a>
    </div>

    <!-- Step progress indicator -->
    <div class="fos-wizard-steps">
        <div class="fos-wizard-step fos-wizard-step--active" id="step-tab-1">
            <div class="fos-wizard-step__num" id="step-num-1"><?php echo esc_html( $is_key_set ? '✓' : '1' ); ?></div>
            <div class="fos-wizard-step__label"><?php esc_html_e('AI Provider', 'contentosapp-studio'); ?></div>
        </div>
        <div class="fos-wizard-step <?php echo esc_attr( $is_key_set ? '' : 'fos-wizard-step--pending' ); ?>" id="step-tab-2">
            <div class="fos-wizard-step__num" id="step-num-2">2</div>
            <div class="fos-wizard-step__label"><?php esc_html_e('Connect site', 'contentosapp-studio'); ?></div>
        </div>
        <div class="fos-wizard-step fos-wizard-step--pending" id="step-tab-3">
            <div class="fos-wizard-step__num" id="step-num-3">3</div>
            <div class="fos-wizard-step__label"><?php esc_html_e('Ready to produce', 'contentosapp-studio'); ?></div>
        </div>
    </div>

    <!-- Step 1: AI Provider -->
    <div class="fos-wizard-panel is-active" id="panel-1">
        <div class="fos-wizard-card">
            <div class="fos-wizard-card__header">
                <div class="fos-wizard-card__title"><?php esc_html_e('Choose your AI provider', 'contentosapp-studio'); ?></div>
                <div class="fos-wizard-card__sub"><?php esc_html_e('The engine that powers all 7 agents. You\'ll need a free API key — takes about 2 minutes to get one.', 'contentosapp-studio'); ?></div>
            </div>

            <div class="fos-wizard-card__body">
                <div class="fos-wizard-providers">
                    <!-- Super Card ContentOS Auto — available on ALL plans -->
                    <div class="fos-wizard-provider <?php echo esc_attr( $current_provider === 'contentos' ? 'is-selected' : '' ); ?>" data-provider="contentos">
                        <div class="fos-wizard-provider__logo fos-wizard-provider__logo--contentos">★</div>
                        <div class="fos-wizard-provider__content">
                            <div style="display:flex; align-items:center; gap:8px;">
                                <div class="fos-wizard-provider__name">ContentOS Auto</div>
                                <span class="fos-wizard-provider__badge" style="background:var(--fos-primary);color:#fff;border:none;"><?php esc_html_e('Recommended', 'contentosapp-studio'); ?></span>
                                <span class="fos-wizard-provider__badge" style="background:#e8f5e9;color:#2e7d32;border:none;"><?php esc_html_e('Free', 'contentosapp-studio'); ?></span>
                            </div>
                            <div class="fos-wizard-provider__hint"><?php esc_html_e('1-click setup. Unified engine for Text, Image and Web Search agents.', 'contentosapp-studio'); ?></div>
                        </div>
                        <input type="radio" name="ai_provider" value="contentos" style="display:none" <?php checked($current_provider, 'contentos'); ?>>
                    </div>

                    <div class="fos-wizard-provider <?php echo esc_attr( $current_provider === 'gemini' ? 'is-selected' : '' ); ?>" data-provider="gemini">
                        <div class="fos-wizard-provider__logo fos-wizard-provider__logo--gemini">G</div>
                        <div class="fos-wizard-provider__name">Google Gemini</div>
                        <span class="fos-wizard-provider__badge fos-wizard-provider__badge--free"><?php esc_html_e('Free tier available', 'contentosapp-studio'); ?></span>
                        <div class="fos-wizard-provider__hint"><?php esc_html_e('Bring your own API key — free tier covers most users', 'contentosapp-studio'); ?></div>
                        <input type="radio" name="ai_provider" value="gemini" style="display:none" <?php checked($current_provider, 'gemini'); ?>>
                    </div>

                    <div class="fos-wizard-provider <?php echo esc_attr( $current_provider === 'openai' ? 'is-selected' : '' ); ?>" data-provider="openai">
                        <div class="fos-wizard-provider__logo fos-wizard-provider__logo--openai">⬡</div>
                        <div class="fos-wizard-provider__name">OpenAI</div>
                        <span class="fos-wizard-provider__badge fos-wizard-provider__badge--paid"><?php esc_html_e('Pay as you go', 'contentosapp-studio'); ?></span>
                        <div class="fos-wizard-provider__hint"><?php esc_html_e('GPT-4o · excellent for English content', 'contentosapp-studio'); ?></div>
                        <input type="radio" name="ai_provider" value="openai" style="display:none" <?php checked($current_provider, 'openai'); ?>>
                    </div>

                    <div class="fos-wizard-provider <?php echo esc_attr( $current_provider === 'claude' ? 'is-selected' : '' ); ?>" data-provider="claude">
                        <div class="fos-wizard-provider__logo fos-wizard-provider__logo--claude">✦</div>
                        <div class="fos-wizard-provider__name">Anthropic</div>
                        <span class="fos-wizard-provider__badge fos-wizard-provider__badge--paid"><?php esc_html_e('Pay as you go', 'contentosapp-studio'); ?></span>
                        <div class="fos-wizard-provider__hint"><?php esc_html_e('Claude · best for long-form articles', 'contentosapp-studio'); ?></div>
                        <input type="radio" name="ai_provider" value="claude" style="display:none" <?php checked($current_provider, 'claude'); ?>>
                    </div>
                </div>

                <div class="fos-wizard-freetier" id="freetier-banner" style="<?php echo esc_attr( $current_provider === 'gemini' ? '' : 'display:none' ); ?>">
                    <div class="fos-wizard-freetier__icon">🎁</div>
                    <div class="fos-wizard-freetier__text">
                        <strong><?php esc_html_e('No API key yet?', 'contentosapp-studio'); ?></strong> 
                        <?php 
                        printf(
                            /* translators: 1: opening link tag, 2: closing link tag */
                            esc_html__('Get your free Gemini key in 2 minutes at %1$saistudio.google.com%2$s — no credit card required. The free tier handles up to 1,500 requests/day.', 'contentosapp-studio'),
                            '<a class="fos-wizard-freetier__link" href="https://aistudio.google.com" target="_blank">',
                            '</a>'
                        );
                        ?>
                    </div>
                </div>

                <div class="fos-wizard-field" id="fos-wiz-apikey-wrap" style="display: <?php echo esc_attr( $current_provider === 'contentos' ? 'none' : 'block' ); ?>">
                    <label class="fos-wizard-field__label"><?php esc_html_e('API Key', 'contentosapp-studio'); ?></label>
                    <div class="fos-wizard-field__row">
                        <input class="fos-wizard-field__input" type="password" id="fos-wiz-apikey" value="<?php echo esc_attr($masked_key); ?>" placeholder="<?php esc_attr_e('Paste your API key here...', 'contentosapp-studio'); ?>" autocomplete="off">
                        <button class="fos-wizard-test-btn" id="fos-wiz-test-btn">
                            ⚡ <?php esc_html_e('Test connection', 'contentosapp-studio'); ?>
                        </button>
                    </div>
                    <div class="fos-wizard-field__hint">
                        🔒 <?php esc_html_e('Stored encrypted in your database. Never shared externally.', 'contentosapp-studio'); ?>
                    </div>

                    <div class="fos-wizard-test-result" id="fos-wiz-result-loading">
                        <div class="fos-wizard-spinner"></div>
                        <?php esc_html_e('Testing connection...', 'contentosapp-studio'); ?>
                    </div>
                    <div class="fos-wizard-test-result <?php echo esc_attr( $is_key_set ? 'is-ok' : '' ); ?>" id="fos-wiz-result-ok" style="<?php echo esc_attr( $is_key_set ? 'display:flex' : 'display:none' ); ?>">
                        ✓ <?php esc_html_e('Connection successful', 'contentosapp-studio'); ?>
                    </div>
                    <div class="fos-wizard-test-result is-err" id="fos-wiz-result-err" style="display:none">
                        ✕ <?php esc_html_e('Invalid key or quota exceeded. Check and try again.', 'contentosapp-studio'); ?>
                    </div>
                </div>

                <!-- BYOK Advanced Options (Hidden when ContentOS Auto is selected) -->
                <div id="fos-wiz-byok-advanced" style="margin-top:20px; display: <?php echo esc_attr( $current_provider === 'contentos' ? 'none' : 'block' ); ?>">
                    <details class="fos-wizard-byok-details" style="border:1px solid var(--fos-border); border-radius:var(--fos-radius-sm); padding:10px 14px; background:#fafafa;">
                        <summary style="cursor:pointer; font-weight:600; font-size:14px; color:var(--fos-text);">⚙️ <?php esc_html_e('Advanced: Image & Web Search Providers', 'contentosapp-studio'); ?></summary>
                        <div style="margin-top:14px; display:flex; flex-direction:column; gap:16px;">
                            <div class="fos-wizard-field">
                                <label class="fos-wizard-field__label"><?php esc_html_e('Image Provider', 'contentosapp-studio'); ?></label>
                                <select id="fos-wiz-img-provider" class="fos-wizard-field__input">
                                    <option value="">— <?php esc_html_e('I will configure later', 'contentosapp-studio'); ?> —</option>
                                    <option value="gemini_imagen">Google Gemini Imagen 3</option>
                                    <option value="openai_dalle">OpenAI DALL-E 3</option>
                                </select>
                                <input class="fos-wizard-field__input" type="password" id="fos-wiz-img-apikey" placeholder="<?php esc_attr_e('Image API Key', 'contentosapp-studio'); ?>" style="margin-top:8px;">
                            </div>
                            <div class="fos-wizard-field">
                                <label class="fos-wizard-field__label"><?php esc_html_e('Web Search Provider', 'contentosapp-studio'); ?></label>
                                <select id="fos-wiz-search-provider" class="fos-wizard-field__input">
                                    <option value="">— <?php esc_html_e('I will configure later', 'contentosapp-studio'); ?> —</option>
                                    <option value="tavily">Tavily AI</option>
                                    <option value="serper">Serper.dev</option>
                                    <option value="exa">Exa.ai Search</option>
                                </select>
                                <input class="fos-wizard-field__input" type="password" id="fos-wiz-search-apikey" placeholder="<?php esc_attr_e('Search API Key', 'contentosapp-studio'); ?>" style="margin-top:8px;">
                            </div>
                        </div>
                    </details>
                </div>
            </div>

            <div class="fos-wizard-card__footer">
                <span class="fos-wizard-step-hint"><?php esc_html_e('Step 1 of 3', 'contentosapp-studio'); ?></span>
                <div style="display:flex;flex-direction:column;align-items:flex-end;gap:4px">
                    <?php if ( ! $is_key_set && $current_provider !== 'contentos' ) : ?>
                        <span id="fos-wiz-btn-1-hint" style="font-size:11px;color:var(--fos-text-dim)">
                            <?php esc_html_e('Enter and test your API key to continue.', 'contentosapp-studio'); ?>
                        </span>
                    <?php endif; ?>
                    <button class="fos-wizard-btn fos-wizard-btn--primary" id="fos-wiz-btn-1" <?php disabled(! $is_key_set && $current_provider !== 'contentos'); ?>>
                        <?php esc_html_e('Continue →', 'contentosapp-studio'); ?>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- Step 2: Connect Site -->
    <div class="fos-wizard-panel" id="panel-2">
        <div class="fos-wizard-card">
            <div class="fos-wizard-card__header">
                <div class="fos-wizard-card__title"><?php esc_html_e('Connect your WordPress site', 'contentosapp-studio'); ?></div>
                <div class="fos-wizard-card__sub"><?php esc_html_e('Where should Contentosapp Studio publish articles? Connect via WordPress Application Passwords — no plugins needed on the destination site.', 'contentosapp-studio'); ?></div>
            </div>

            <div class="fos-wizard-card__body">
                <div id="fos-wiz-sites-container">
                    <?php
                    $local_key = defined('Costudio_WP_Publisher::LOCAL_SITE_KEY')
                        ? Costudio_WP_Publisher::LOCAL_SITE_KEY
                        : '__local__';
                    // Consider local already connected if __local__ key exists OR if the home URL
                    // matches any already-connected site (added via credentials).
                    $local_home_url = trailingslashit( strtolower( home_url() ) );
                    $local_url_match_key = '';
                    foreach ( $connected_sites as $_ck => $_cs ) {
                        if ( trailingslashit( strtolower( $_cs['url'] ?? '' ) ) === $local_home_url ) {
                            $local_url_match_key = $_ck;
                            break;
                        }
                    }
                    $local_already_connected = isset($connected_sites[$local_key]) || $local_url_match_key !== '';
                    ?>
                    <?php if ( isset( $connected_sites[$local_key] ) ) : ?>
                        <div class="fos-wizard-site-connected fos-wizard-site-local" data-site-key="<?php echo esc_attr($local_key); ?>">
                            <div class="fos-wizard-site-dot"></div>
                            <div>
                                <div class="fos-wizard-site-name">
                                    <?php echo esc_html($connected_sites[$local_key]['name'] ?? get_bloginfo('name')); ?>
                                    <span class="fos-wizard-local-badge"><?php esc_html_e('This site', 'contentosapp-studio'); ?></span>
                                </div>
                                <div class="fos-wizard-site-url"><?php echo esc_html(home_url()); ?> · <?php esc_html_e('Auto-connected · no credentials needed', 'contentosapp-studio'); ?></div>
                            </div>
                            <div class="fos-wizard-site-actions">
                                <span class="fos-wizard-site-remove" data-key="<?php echo esc_attr($local_key); ?>">✕ <?php esc_html_e('Remove', 'contentosapp-studio'); ?></span>
                            </div>
                        </div>
                    <?php elseif ( $local_url_match_key === '' ) : ?>
                        <div class="fos-wizard-local-connect" id="fos-wiz-local-connect-card">
                            <div class="fos-wizard-local-connect__icon">⚡</div>
                            <div class="fos-wizard-local-connect__body">
                                <div class="fos-wizard-local-connect__title"><?php esc_html_e('Connect this site', 'contentosapp-studio'); ?> <span class="fos-wizard-local-badge"><?php esc_html_e('Plug & Play', 'contentosapp-studio'); ?></span></div>
                                <div class="fos-wizard-local-connect__url"><?php echo esc_html(home_url()); ?></div>
                                <div class="fos-wizard-local-connect__sub"><?php esc_html_e('No credentials needed — publishes directly to this WordPress installation.', 'contentosapp-studio'); ?></div>
                            </div>
                            <button type="button" class="fos-wizard-btn fos-wizard-btn--primary" id="fos-wiz-local-connect-btn">
                                <?php esc_html_e('Auto-connect →', 'contentosapp-studio'); ?>
                            </button>
                        </div>
                    <?php endif; ?>
                    <?php if ($has_sites) : ?>
                        <?php foreach ($connected_sites as $site_key => $site) : ?>
                            <?php if ( $site_key === $local_key ) continue; ?>
                            <div class="fos-wizard-site-connected"
                                data-site-key="<?php echo esc_attr($site_key); ?>"
                                data-site-name="<?php echo esc_attr($site['name'] ?? $site['label'] ?? ''); ?>"
                                data-site-url="<?php echo esc_attr($site['url'] ?? ''); ?>"
                                data-site-user="<?php echo esc_attr($site['user'] ?? ''); ?>">
                                <div class="fos-wizard-site-dot"></div>
                                <div>
                                    <div class="fos-wizard-site-name"><?php echo esc_html(costudio_site_display_name($site, $site_key)); ?></div>
                                    <div class="fos-wizard-site-url"><?php echo esc_html($site['url'] ?? ''); ?> · <?php esc_html_e( 'Connected successfully', 'contentosapp-studio' ); ?></div>
                                </div>
                                <div class="fos-wizard-site-actions">
                                    <span class="fos-wizard-site-edit">✎ <?php esc_html_e('Edit', 'contentosapp-studio'); ?></span>
                                    <span class="fos-wizard-site-remove">✕ <?php esc_html_e('Remove', 'contentosapp-studio'); ?></span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>

                <div class="fos-wizard-site-add" id="fos-wiz-site-add-btn" style="<?php echo esc_attr( $can_add_site ? '' : 'display:none' ); ?>">
                    <div class="fos-wizard-site-add__icon">+</div>
                    <div>
                        <div class="fos-wizard-site-add__label"><?php esc_html_e('Add a WordPress site', 'contentosapp-studio'); ?></div>
                        <div class="fos-wizard-site-add__sub"><?php esc_html_e('URL + Application Password — 2 minutes to set up', 'contentosapp-studio'); ?></div>
                    </div>
                </div>

                <div class="fos-wizard-site-limit-notice" id="fos-wiz-site-limit-notice" style="display:none"></div>

                <input type="hidden" id="fos-wiz-max-sites" value="<?php echo esc_attr($max_sites); ?>">

                <input type="hidden" id="fos-wiz-editing-key" value="">

                <div class="fos-wizard-site-form" id="fos-wiz-site-form">
                    <div class="fos-wizard-field">
                        <label class="fos-wizard-field__label"><?php esc_html_e('Site name', 'contentosapp-studio'); ?></label>
                        <input class="fos-wizard-field__input" type="text" id="fos-wiz-site-name" placeholder="<?php esc_attr_e('Ex: My Blog', 'contentosapp-studio'); ?>">
                    </div>
                    <div class="fos-wizard-field">
                        <label class="fos-wizard-field__label"><?php esc_html_e('Site URL', 'contentosapp-studio'); ?></label>
                        <input class="fos-wizard-field__input" type="url" id="fos-wiz-site-url" placeholder="https://meusite.com.br">
                    </div>
                    <div class="fos-wizard-field">
                        <label class="fos-wizard-field__label"><?php esc_html_e('WordPress username', 'contentosapp-studio'); ?></label>
                        <input class="fos-wizard-field__input" type="text" id="fos-wiz-site-user" placeholder="admin">
                    </div>
                    <div class="fos-wizard-field">
                        <label class="fos-wizard-field__label"><?php esc_html_e('Application Password', 'contentosapp-studio'); ?></label>
                        <input class="fos-wizard-field__input" type="password" id="fos-wiz-site-pass" placeholder="xxxx xxxx xxxx xxxx xxxx xxxx" data-placeholder-new="xxxx xxxx xxxx xxxx xxxx xxxx" data-placeholder-edit="<?php esc_attr_e('Leave blank to keep current', 'contentosapp-studio'); ?>">
                        <div class="fos-wizard-apppass-help">
                            <?php 
                            printf(
                                /* translators: 1: bold opening tag, 2: bold closing tag, 3: opening link tag, 4: closing link tag */
                                esc_html__('Generate in: %1$sWordPress Admin → Users → Profile → Application Passwords%2$s. %3$sWatch 60s tutorial →%4$s', 'contentosapp-studio'),
                                '<strong>', '</strong>', '<a href="#" target="_blank">', '</a>'
                            );
                            ?>
                        </div>
                    </div>
                    <div class="fos-wizard-field">
                        <label class="fos-wizard-field__label"><?php esc_html_e('Site Theme Color', 'contentosapp-studio'); ?></label>
                        <div style="display:flex; align-items:center; gap:12px; background:#fafafa; border:1px solid var(--fos-border); padding:8px 12px; border-radius:var(--fos-radius-sm);">
                            <input type="color" id="fos-wiz-site-color" value="#6C47FF" style="width:40px; height:40px; min-width:40px; min-height:40px; padding:2px; cursor:pointer; border-radius:4px; border:1px solid #ccc; flex-shrink:0; background:none;">
                            <p style="margin:0; font-size:12px; color:var(--fos-text-light); line-height:1.4;">
                                <?php esc_html_e('Used by ContentOS Agents to colorize HTML highlight boxes and stylize generated images to match this site\'s branding.', 'contentosapp-studio'); ?>
                            </p>
                        </div>
                    </div>
                    <!-- Categories area -->
                    <div class="fos-cat-area" style="margin-top:14px">
                        <div class="fos-cat-area__header">
                            <span class="fos-cat-area__label">🏷️ <?php esc_html_e('Available categories', 'contentosapp-studio'); ?></span>
                            <button type="button" id="fos-wiz-load-cats-btn" class="fos-cat-area__load-btn">
                                🔄 <?php esc_html_e('Load from site', 'contentosapp-studio'); ?>
                            </button>
                        </div>
                        <div class="fos-cat-area__body" id="fos-wiz-cat-tags-wrap">
                            <span class="fos-cat-area__empty"><?php esc_html_e('Enter the URL above and click "Load from site".', 'contentosapp-studio'); ?></span>
                        </div>
                        <p class="fos-form-field__hint">
                            🌐 <?php esc_html_e('Categories are public on WordPress — no credentials needed to load them.', 'contentosapp-studio'); ?>
                        </p>
                    </div>

                    <div style="display:flex;gap:8px;margin-top:14px">
                        <button class="fos-wizard-btn fos-wizard-btn--primary" id="fos-wiz-save-site">⚡ <?php esc_html_e('Test & connect', 'contentosapp-studio'); ?></button>
                        <button class="fos-wizard-btn fos-wizard-btn--ghost" id="fos-wiz-cancel-site"><?php esc_html_e('Cancel', 'contentosapp-studio'); ?></button>
                    </div>
                </div>

                <a class="fos-wizard-skip" href="#" id="fos-wiz-skip-2">
                    <?php esc_html_e('Skip for now — I\'ll connect a site later in Settings', 'contentosapp-studio'); ?>
                </a>
            </div>

            <div class="fos-wizard-card__footer">
                <button class="fos-wizard-btn fos-wizard-btn--ghost" id="fos-wiz-back-2">← <?php esc_html_e('Back', 'contentosapp-studio'); ?></button>
                <div style="display:flex;align-items:center;gap:12px">
                    <span class="fos-wizard-step-hint"><?php esc_html_e('Step 2 of 3', 'contentosapp-studio'); ?></span>
                    <button class="fos-wizard-btn fos-wizard-btn--primary" id="fos-wiz-btn-2">
                        <?php esc_html_e('Continue →', 'contentosapp-studio'); ?>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- Step 3: Ready -->
    <div class="fos-wizard-panel" id="panel-3">
        <div class="fos-wizard-card">
            <div class="fos-wizard-ready-hero">
                <div class="fos-wizard-ready-hero__icon">🚀</div>
                <div class="fos-wizard-ready-hero__title">
                    <?php echo esc_html( $setup_complete ? __( 'You\'re ready to produce content', 'contentosapp-studio' ) : __( 'Almost ready — one thing missing', 'contentosapp-studio' ) ); ?>
                </div>
                <div class="fos-wizard-ready-hero__sub">
                    <?php echo esc_html( $setup_complete ? __( 'Contentosapp Studio is configured. Start your first production and watch all 7 agents work in sequence.', 'contentosapp-studio' ) : __( 'Your AI provider is set up. Connect a WordPress site to start publishing directly.', 'contentosapp-studio' ) ); ?>
                </div>
            </div>

            <div class="fos-wizard-checklist">
                <div class="fos-wizard-check">
                    <div class="fos-wizard-check__dot fos-wizard-check__dot--ok">✓</div>
                    <div class="fos-wizard-check__text" id="fos-wiz-summary-ai">
                        <strong><?php esc_html_e('AI Provider', 'contentosapp-studio'); ?></strong> — <?php
                            $provider_label = $current_provider;
                            if ( $ai_mode === 'contentos' || $current_provider === 'contentos' ) {
                                $provider_label = 'ContentOS Auto';
                            } elseif ( $current_provider === 'gemini' ) {
                                $provider_label = 'Google Gemini';
                            } elseif ( $current_provider === 'openai' ) {
                                $provider_label = 'OpenAI';
                            } elseif ( $current_provider === 'claude' ) {
                                $provider_label = 'Anthropic Claude';
                            }
                            echo esc_html( $provider_label );
                        ?> <?php esc_html_e('configured and tested', 'contentosapp-studio'); ?>
                    </div>
                </div>

                <div class="fos-wizard-check">
                    <div class="fos-wizard-check__dot <?php echo esc_attr( $has_sites ? 'fos-wizard-check__dot--ok' : 'fos-wizard-check__dot--warn' ); ?>" id="fos-wiz-summary-site-dot">
                        <?php echo esc_html( $has_sites ? '✓' : '!' ); ?>
                    </div>
                    <div class="fos-wizard-check__text" id="fos-wiz-summary-site">
                        <strong><?php esc_html_e('WordPress site', 'contentosapp-studio'); ?></strong> — 
                        <?php if ($has_sites) : ?>
                            <?php echo esc_html(count($operable_sites)); ?> / <?php echo esc_html(count($connected_sites)); ?> <?php esc_html_e('site(s) available', 'contentosapp-studio'); ?>
                        <?php else : ?>
                            <?php esc_html_e('No site connected — articles will be saved as drafts only.', 'contentosapp-studio'); ?>
                            <a href="#panel-2" id="fos-wiz-add-site-from-summary" class="fos-wizard-inline-action"><?php esc_html_e('Adicionar um site nas Configurações →', 'contentosapp-studio'); ?></a>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="fos-wizard-check" id="fos-wiz-summary-img-wrap">
                    <div class="fos-wizard-check__dot fos-wizard-check__dot--skip" id="fos-wiz-summary-img-dot">—</div>
                    <div class="fos-wizard-check__text" id="fos-wiz-summary-img">
                        <strong><?php esc_html_e('Image provider', 'contentosapp-studio'); ?></strong> — <?php esc_html_e('not configured. Articles will skip image generation.', 'contentosapp-studio'); ?>
                        <a href="<?php echo esc_url( admin_url('admin.php?page=costudio-settings') ); ?>" class="fos-wizard-exit" style="color:var(--fos-primary);font-weight:500"><?php esc_html_e('Configure later in Settings →', 'contentosapp-studio'); ?></a>
                    </div>
                </div>

                <div class="fos-wizard-check" id="fos-wiz-summary-search-wrap">
                    <div class="fos-wizard-check__dot fos-wizard-check__dot--skip" id="fos-wiz-summary-search-dot">—</div>
                    <div class="fos-wizard-check__text" id="fos-wiz-summary-search">
                        <strong><?php esc_html_e('Web Search', 'contentosapp-studio'); ?></strong> — <?php esc_html_e('not configured. Agents will rely on base knowledge.', 'contentosapp-studio'); ?>
                    </div>
                </div>
            </div>

            <!-- ── Privacy consent (binary choice — LGPD/GDPR compliant) ─── -->
            <div class="fos-wizard-privacy-card" id="fos-wiz-privacy-card" style="<?php echo esc_attr( $telemetry_decided ? 'display:none;' : '' ); ?>margin:20px 0;padding:18px 20px;background:#f9fafb;border:1px solid var(--fos-border);border-radius:var(--fos-radius-sm);">
                <div style="display:flex;gap:12px;align-items:flex-start;">
                    <div style="font-size:24px;line-height:1;flex-shrink:0;">📊</div>
                    <div style="flex:1;">
                        <div style="font-weight:600;font-size:14px;color:#111827;margin-bottom:6px;">
                            <?php esc_html_e('How would you like to help us improve?', 'contentosapp-studio'); ?>
                        </div>
                        <p style="margin:0 0 14px;font-size:13px;color:#4b5563;line-height:1.5;">
                            <?php esc_html_e('Sharing anonymous usage data (models, latency, article types) helps us prioritize what you use most. We never send your content, keywords or credentials.', 'contentosapp-studio'); ?>
                        </p>
                        <div style="display:flex;gap:10px;flex-wrap:wrap;">
                            <button type="button" class="fos-wizard-btn fos-wizard-btn--primary" id="fos-wiz-privacy-accept" data-consent="1">
                                ✓ <?php esc_html_e('Accept and help', 'contentosapp-studio'); ?>
                            </button>
                            <button type="button" class="fos-wizard-btn fos-wizard-btn--ghost" id="fos-wiz-privacy-decline" data-consent="0">
                                <?php esc_html_e('No, thanks', 'contentosapp-studio'); ?>
                            </button>
                            <a href="#" id="fos-wiz-privacy-details" style="margin-left:auto;align-self:center;font-size:12px;color:var(--fos-primary);text-decoration:none;">
                                <?php esc_html_e('See details', 'contentosapp-studio'); ?>
                            </a>
                        </div>
                        <p style="margin:10px 0 0;font-size:11px;color:#6b7280;">
                            <?php esc_html_e('You can change this later in Settings → Advanced.', 'contentosapp-studio'); ?>
                        </p>
                    </div>
                </div>
            </div>

            <!-- ── Privacy "decision saved" feedback (hidden until choice is made) ─── -->
            <div class="fos-wizard-privacy-confirmed" id="fos-wiz-privacy-confirmed" style="<?php echo esc_attr( $telemetry_decided ? '' : 'display:none;' ); ?>margin:14px 0;padding:10px 14px;background:#ecfdf5;border:1px solid #a7f3d0;border-radius:var(--fos-radius-sm);font-size:12px;color:#065f46;">
                ✓ <?php esc_html_e('Privacy preference saved. You can change it anytime in Settings → Advanced.', 'contentosapp-studio'); ?>
            </div>

            <div class="fos-wizard-ready-nav" aria-label="<?php esc_attr_e('Wizard navigation', 'contentosapp-studio'); ?>">
                <button type="button" class="fos-wizard-btn fos-wizard-btn--ghost" id="fos-wiz-back-3-provider">
                    <?php esc_html_e('← Voltar ao Provedor de IA', 'contentosapp-studio'); ?>
                </button>
                <button type="button" class="fos-wizard-btn fos-wizard-btn--ghost" id="fos-wiz-back-3-site">
                    <?php esc_html_e('← Voltar para Conectar site', 'contentosapp-studio'); ?>
                </button>
            </div>

            <div class="fos-wizard-ready-footer">
                <a href="<?php echo esc_url( admin_url('admin.php?page=costudio-studio&tab=producao') ); ?>" class="fos-wizard-cta" id="fos-wiz-cta-primary" style="text-decoration:none;<?php echo esc_attr( $telemetry_decided ? '' : 'pointer-events:none;opacity:0.5;cursor:not-allowed;' ); ?>" data-needs-consent="1">
                    🚀 <?php esc_html_e('Start my first production', 'contentosapp-studio'); ?>
                </a>
                <a class="fos-wizard-skip" id="fos-wiz-cta-secondary" style="margin-top:0;<?php echo esc_attr( $telemetry_decided ? '' : 'pointer-events:none;opacity:0.5;cursor:not-allowed;' ); ?>" href="<?php echo esc_url( admin_url('admin.php?page=costudio') ); ?>" data-needs-consent="1">
                    <?php esc_html_e('Go to dashboard instead', 'contentosapp-studio'); ?>
                </a>
            </div>

            <!-- ── Privacy details modal ──────────────────────────────────────── -->
            <div id="fos-wiz-privacy-modal" style="display:none;position:fixed;inset:0;z-index:100000;background:rgba(0,0,0,0.5);align-items:center;justify-content:center;">
                <div class="fos-modal-card">
                    <div class="fos-modal-card__header">
                        <div class="fos-modal-card__title-wrap">
                            <span class="fos-modal-card__hicon">📊</span>
                            <span class="fos-modal-card__title"><?php esc_html_e('What we collect', 'contentosapp-studio'); ?></span>
                        </div>
                        <button type="button" class="fos-modal-card__close" id="fos-wiz-privacy-modal-x" aria-label="<?php esc_attr_e('Close', 'contentosapp-studio'); ?>">&times;</button>
                    </div>
                    <div class="fos-modal-card__body">
                        <div class="fos-modal-card__section fos-modal-card__section--ok">
                            <p class="fos-modal-card__section-title">✓ <?php esc_html_e('What we send', 'contentosapp-studio'); ?></p>
                            <ul class="fos-modal-card__list">
                                <li><?php esc_html_e('AI models used (e.g. Gemini, Claude)', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Article type (Pillar, News, etc.)', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Output language', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Tokens consumed and execution time', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Anonymous site identifier (irreversible hash)', 'contentosapp-studio'); ?></li>
                            </ul>
                        </div>
                        <div class="fos-modal-card__section fos-modal-card__section--no">
                            <p class="fos-modal-card__section-title">✗ <?php esc_html_e('What we never send', 'contentosapp-studio'); ?></p>
                            <ul class="fos-modal-card__list">
                                <li><?php esc_html_e('Article content', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Keywords or production briefings', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Credentials (API keys, passwords)', 'contentosapp-studio'); ?></li>
                                <li><?php esc_html_e('Real site URLs', 'contentosapp-studio'); ?></li>
                            </ul>
                        </div>
                    </div>
                    <div class="fos-modal-card__footer">
                        <button type="button" id="fos-wiz-privacy-modal-close" class="fos-modal-card__btn--primary">
                            <?php esc_html_e('Got it', 'contentosapp-studio'); ?>
                        </button>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

<input type="hidden" id="fos-wizard-nonce" value="<?php echo esc_attr($nonce); ?>">
