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

/**
 * 初期セットアップウィザード のレンダリング
 *
 * 元 admin/class-aiap-admin-view.php から分離。
 * 共通基盤 (resolve_dynamic_key, log_message 等) は親クラス AIAP_Admin_View にあるため直接参照する。
 */
class AIAP_Admin_View_Wizard {

    // ================================================================
    // 基本設定 (unchanged from original)
    // ================================================================
    // ================================================================
    // Phase 3: Quick Start Wizard
    // ================================================================
    public static function render_wizard() {
        ?>
        <div class="wrap aiap-wrap aiap-theme-modern" data-aiap-ajax-url="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" data-aiap-nonce="<?php echo esc_attr( wp_create_nonce( AIAP_Admin::NONCE ) ); ?>">
            <div class="aiap-wizard">
                <header class="aiap-wizard-header">
                    <div class="aiap-wizard-eyebrow"><?php echo esc_html( __( 'Quick Start', 'ai-auto-poster' ) ); ?></div>
                    <h1 class="aiap-wizard-title"><?php echo esc_html( __( 'Welcome to AI Auto Poster', 'ai-auto-poster' ) ); ?></h1>
                    <p class="aiap-wizard-sub"><?php echo esc_html( __( 'Set up automatic posting in 4 steps.', 'ai-auto-poster' ) ); ?></p>
                    <div class="aiap-wizard-progress" data-aiap-wizard-progress>
                        <div class="aiap-wizard-progress-fill" data-aiap-wizard-progress-fill></div>
                    </div>
                    <div class="aiap-wizard-stepnums">
                        <span class="aiap-wizard-stepnum is-active" data-stepnum="1">1</span>
                        <span class="aiap-wizard-stepnum" data-stepnum="2">2</span>
                        <span class="aiap-wizard-stepnum" data-stepnum="3">3</span>
                        <span class="aiap-wizard-stepnum" data-stepnum="4">4</span>
                    </div>
                </header>

                <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="aiap-wizard-form" data-aiap-wizard-form>
                    <input type="hidden" name="action" value="aiap_wizard_complete">
                    <?php wp_nonce_field( AIAP_Admin::NONCE ); ?>

                    <!-- Step 1: theme / prompt -->
                    <section class="aiap-wizard-step is-active" data-aiap-wizard-step="1">
                        <h2 class="aiap-wizard-step-title"><?php echo esc_html( __( 'What do you want to post about?', 'ai-auto-poster' ) ); ?></h2>
                        <p class="aiap-wizard-step-sub"><?php echo esc_html( __( 'Pick a preset, or describe your topic in your own words.', 'ai-auto-poster' ) ); ?></p>

                        <div class="aiap-wizard-presets">
                            <button type="button" class="aiap-wizard-preset" data-aiap-wizard-preset="<?php echo esc_attr( __( 'Daily small observations and discoveries about nature, written in a warm, gentle tone.', 'ai-auto-poster' ) ); ?>">
                                <span class="aiap-wizard-preset-icon">🌿</span>
                                <span class="aiap-wizard-preset-name"><?php echo esc_html( __( 'Nature observations', 'ai-auto-poster' ) ); ?></span>
                            </button>
                            <button type="button" class="aiap-wizard-preset" data-aiap-wizard-preset="<?php echo esc_attr( __( 'Practical business tips for small companies and freelancers, focused on productivity and growth.', 'ai-auto-poster' ) ); ?>">
                                <span class="aiap-wizard-preset-icon">💼</span>
                                <span class="aiap-wizard-preset-name"><?php echo esc_html( __( 'Business tips', 'ai-auto-poster' ) ); ?></span>
                            </button>
                            <button type="button" class="aiap-wizard-preset" data-aiap-wizard-preset="<?php echo esc_attr( __( 'Hobby and lifestyle topics with a casual, friendly voice.', 'ai-auto-poster' ) ); ?>">
                                <span class="aiap-wizard-preset-icon">🎨</span>
                                <span class="aiap-wizard-preset-name"><?php echo esc_html( __( 'Hobbies & lifestyle', 'ai-auto-poster' ) ); ?></span>
                            </button>
                            <button type="button" class="aiap-wizard-preset" data-aiap-wizard-preset="">
                                <span class="aiap-wizard-preset-icon">✏</span>
                                <span class="aiap-wizard-preset-name"><?php echo esc_html( __( 'Custom', 'ai-auto-poster' ) ); ?></span>
                            </button>
                        </div>

                        <div class="aiap-field">
                            <label class="aiap-field-label" for="aiap-wiz-prompt"><?php echo esc_html( __( 'Prompt (what the AI should write about)', 'ai-auto-poster' ) ); ?> <span class="aiap-required">*</span></label>
                            <textarea id="aiap-wiz-prompt" name="wiz_prompt" rows="3" class="aiap-input-text aiap-editor-textarea" placeholder="<?php echo esc_attr( __( 'e.g. Daily small observations about nature.', 'ai-auto-poster' ) ); ?>" required></textarea>
                        </div>

                        <div class="aiap-wizard-actions">
                            <span></span>
                            <button type="button" class="button button-primary aiap-btn-primary" data-aiap-wizard-next><?php echo esc_html( __( 'Next', 'ai-auto-poster' ) ); ?> →</button>
                        </div>
                    </section>

                    <!-- Step 2: frequency -->
                    <section class="aiap-wizard-step" data-aiap-wizard-step="2">
                        <h2 class="aiap-wizard-step-title"><?php echo esc_html( __( 'How often should it post?', 'ai-auto-poster' ) ); ?></h2>
                        <p class="aiap-wizard-step-sub"><?php echo esc_html( __( 'You can adjust this later in the task settings.', 'ai-auto-poster' ) ); ?></p>

                        <div class="aiap-wizard-options">
                            <label class="aiap-wizard-option">
                                <input type="radio" name="wiz_freq" value="daily" checked>
                                <span class="aiap-wizard-option-body">
                                    <span class="aiap-wizard-option-icon">📅</span>
                                    <span class="aiap-wizard-option-name"><?php echo esc_html( __( 'Once a day', 'ai-auto-poster' ) ); ?></span>
                                    <span class="aiap-wizard-option-desc"><?php echo esc_html( __( '1 post every day at 09:00', 'ai-auto-poster' ) ); ?></span>
                                </span>
                            </label>
                            <label class="aiap-wizard-option">
                                <input type="radio" name="wiz_freq" value="random">
                                <span class="aiap-wizard-option-body">
                                    <span class="aiap-wizard-option-icon">🔁</span>
                                    <span class="aiap-wizard-option-name"><?php echo esc_html( __( '5–10 times a day, random', 'ai-auto-poster' ) ); ?></span>
                                    <span class="aiap-wizard-option-desc"><?php echo esc_html( __( 'Random count and times each day (excluding 01:00–06:00)', 'ai-auto-poster' ) ); ?></span>
                                </span>
                            </label>
                            <label class="aiap-wizard-option">
                                <input type="radio" name="wiz_freq" value="weekly">
                                <span class="aiap-wizard-option-body">
                                    <span class="aiap-wizard-option-icon">📌</span>
                                    <span class="aiap-wizard-option-name"><?php echo esc_html( __( 'A few times a week', 'ai-auto-poster' ) ); ?></span>
                                    <span class="aiap-wizard-option-desc"><?php echo esc_html( __( 'Mon / Wed / Fri at 09:00', 'ai-auto-poster' ) ); ?></span>
                                </span>
                            </label>
                        </div>

                        <div class="aiap-wizard-actions">
                            <button type="button" class="button" data-aiap-wizard-prev>← <?php echo esc_html( __( 'Back', 'ai-auto-poster' ) ); ?></button>
                            <button type="button" class="button button-primary aiap-btn-primary" data-aiap-wizard-next><?php echo esc_html( __( 'Next', 'ai-auto-poster' ) ); ?> →</button>
                        </div>
                    </section>

                    <!-- Step 3: provider + API key -->
                    <section class="aiap-wizard-step" data-aiap-wizard-step="3">
                        <h2 class="aiap-wizard-step-title"><?php echo esc_html( __( 'Pick an AI provider', 'ai-auto-poster' ) ); ?></h2>
                        <p class="aiap-wizard-step-sub"><?php echo esc_html( __( 'You can change provider or add more later.', 'ai-auto-poster' ) ); ?></p>

                        <div class="aiap-field">
                            <label class="aiap-field-label" for="aiap-wiz-provider"><?php echo esc_html( __( 'Provider', 'ai-auto-poster' ) ); ?></label>
                            <select id="aiap-wiz-provider" name="wiz_provider" class="aiap-select">
                                <option value="anthropic" selected>Anthropic (Claude)</option>
                                <option value="openai">OpenAI (GPT)</option>
                                <option value="gemini">Google (Gemini)</option>
                            </select>
                        </div>
                        <div class="aiap-field">
                            <label class="aiap-field-label" for="aiap-wiz-apikey"><?php echo esc_html( __( 'API Key', 'ai-auto-poster' ) ); ?></label>
                            <input type="password" id="aiap-wiz-apikey" name="wiz_apikey" class="aiap-input-text" autocomplete="new-password" placeholder="sk-...">
                            <span class="aiap-field-hint"><?php echo esc_html( __( 'You can skip this step and set it up later from the Provider tab.', 'ai-auto-poster' ) ); ?></span>
                        </div>

                        <div class="aiap-wizard-actions">
                            <button type="button" class="button" data-aiap-wizard-prev>← <?php echo esc_html( __( 'Back', 'ai-auto-poster' ) ); ?></button>
                            <button type="button" class="button button-primary aiap-btn-primary" data-aiap-wizard-next><?php echo esc_html( __( 'Next', 'ai-auto-poster' ) ); ?> →</button>
                        </div>
                    </section>

                    <!-- Step 4: review + complete -->
                    <section class="aiap-wizard-step" data-aiap-wizard-step="4">
                        <h2 class="aiap-wizard-step-title"><?php echo esc_html( __( 'Review and start', 'ai-auto-poster' ) ); ?></h2>
                        <p class="aiap-wizard-step-sub"><?php echo esc_html( __( 'Confirm your setup. Everything is editable later.', 'ai-auto-poster' ) ); ?></p>

                        <ul class="aiap-wizard-review" data-aiap-wizard-review>
                            <li><strong><?php echo esc_html( __( 'Prompt', 'ai-auto-poster' ) ); ?>:</strong> <span data-review="prompt">—</span></li>
                            <li><strong><?php echo esc_html( __( 'Frequency', 'ai-auto-poster' ) ); ?>:</strong> <span data-review="freq">—</span></li>
                            <li><strong><?php echo esc_html( __( 'Provider', 'ai-auto-poster' ) ); ?>:</strong> <span data-review="provider">—</span></li>
                            <li><strong><?php echo esc_html( __( 'API Key', 'ai-auto-poster' ) ); ?>:</strong> <span data-review="apikey">—</span></li>
                        </ul>

                        <div class="aiap-wizard-actions">
                            <button type="button" class="button" data-aiap-wizard-prev>← <?php echo esc_html( __( 'Back', 'ai-auto-poster' ) ); ?></button>
                            <button type="submit" class="button button-primary aiap-btn-primary"><?php echo esc_html( __( 'Complete setup', 'ai-auto-poster' ) ); ?></button>
                        </div>
                    </section>

                </form>

                <p class="aiap-wizard-skip">
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=' . AIAP_Admin::MENU_SLUG . '&wizard_skip=1' ) ); ?>"><?php echo esc_html( __( 'Skip setup and configure manually', 'ai-auto-poster' ) ); ?></a>
                </p>
            </div>
        </div>
        <?php
    }

}
