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

$defaults = [
    'id' => 0,
    'topic' => '',
    'working_title' => '',
    'primary_keyword' => '',
    'secondary_keywords' => '',
    'audience' => '',
    'search_intent' => '',
    'category_id' => 0,
    'tags' => '',
    'priority' => 50,
    'risk_level' => 'normal',
    'publish_mode' => 'draft_only',
    'language' => '',
    'status' => 'idea',
    'notes' => '',
    'brief' => null,
];
$topic = wp_parse_args($topic ?: [], $defaults);
$topic_id = (int) $topic['id'];
?>
<div class="wrap aisbw-admin">
    <h1><?php echo $topic_id ? esc_html__('Edit Topic', 'guco-ai-blog-writer') : esc_html__('Add Topic', 'guco-ai-blog-writer'); ?></h1>

    <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=aisbw-topic-edit')); ?>">
        <?php wp_nonce_field('aisbw_save_topic'); ?>
        <input type="hidden" name="aisbw_action" value="save_topic">
        <input type="hidden" name="topic_id" value="<?php echo esc_attr((string) $topic_id); ?>">

        <table class="form-table" role="presentation">
            <tr>
                <th scope="row"><label for="aisbw-topic"><?php esc_html_e('Topic', 'guco-ai-blog-writer'); ?></label></th>
                <td><input id="aisbw-topic" class="regular-text" type="text" name="topic" value="<?php echo esc_attr((string) $topic['topic']); ?>" required></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-working-title"><?php esc_html_e('Working Title', 'guco-ai-blog-writer'); ?></label></th>
                <td><input id="aisbw-working-title" class="regular-text" type="text" name="working_title" value="<?php echo esc_attr((string) $topic['working_title']); ?>"></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-primary-keyword"><?php esc_html_e('Primary Keyword', 'guco-ai-blog-writer'); ?></label></th>
                <td><input id="aisbw-primary-keyword" class="regular-text" type="text" name="primary_keyword" value="<?php echo esc_attr((string) $topic['primary_keyword']); ?>"></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-secondary-keywords"><?php esc_html_e('Secondary Keywords', 'guco-ai-blog-writer'); ?></label></th>
                <td><textarea id="aisbw-secondary-keywords" class="large-text" name="secondary_keywords" rows="2"><?php echo esc_textarea((string) $topic['secondary_keywords']); ?></textarea></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-audience"><?php esc_html_e('Target Audience', 'guco-ai-blog-writer'); ?></label></th>
                <td><input id="aisbw-audience" class="regular-text" type="text" name="audience" value="<?php echo esc_attr((string) $topic['audience']); ?>"></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-search-intent"><?php esc_html_e('Search Intent', 'guco-ai-blog-writer'); ?></label></th>
                <td>
                    <?php
                    $search_intent_options = [
                        'informational' => __('Informacyjna (informational)', 'guco-ai-blog-writer'),
                        'commercial' => __('Komercyjna (commercial)', 'guco-ai-blog-writer'),
                        'transactional' => __('Transakcyjna (transactional)', 'guco-ai-blog-writer'),
                        'navigational' => __('Nawigacyjna (navigational)', 'guco-ai-blog-writer'),
                    ];
                    $current_intent = (string) ($topic['search_intent'] ?? 'informational');
                    if (empty($current_intent)) {
                        $current_intent = 'informational';
                    }
                    ?>
                    <select id="aisbw-search-intent" name="search_intent">
                        <?php foreach ($search_intent_options as $val => $label) : ?>
                            <option value="<?php echo esc_attr($val); ?>" <?php selected($current_intent, $val); ?>><?php echo esc_html($label); ?></option>
                        <?php endforeach; ?>
                        <?php if (!array_key_exists($current_intent, $search_intent_options)) : ?>
                            <option value="<?php echo esc_attr($current_intent); ?>" selected><?php echo esc_html($current_intent); ?></option>
                        <?php endif; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="category_id"><?php esc_html_e('WP Category', 'guco-ai-blog-writer'); ?></label></th>
                <td>
                    <?php
                    wp_dropdown_categories([
                        'name' => 'category_id',
                        'selected' => (int) $topic['category_id'],
                        'show_option_none' => __('None', 'guco-ai-blog-writer'),
                        'option_none_value' => 0,
                        'hide_empty' => false,
                    ]);
                    ?>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-tags"><?php esc_html_e('Tags', 'guco-ai-blog-writer'); ?></label></th>
                <td><input id="aisbw-tags" class="regular-text" type="text" name="tags" value="<?php echo esc_attr((string) $topic['tags']); ?>"></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-priority"><?php esc_html_e('Priority', 'guco-ai-blog-writer'); ?></label></th>
                <td><input id="aisbw-priority" type="number" min="0" max="100" name="priority" value="<?php echo esc_attr((string) $topic['priority']); ?>"></td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-risk-level"><?php esc_html_e('Risk Level', 'guco-ai-blog-writer'); ?></label></th>
                <td>
                    <select id="aisbw-risk-level" name="risk_level">
                        <option value="low" <?php selected($topic['risk_level'], 'low'); ?>>low</option>
                        <option value="normal" <?php selected($topic['risk_level'], 'normal'); ?>>normal</option>
                        <option value="high" <?php selected($topic['risk_level'], 'high'); ?>>high</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-publish-mode"><?php esc_html_e('Publish Mode', 'guco-ai-blog-writer'); ?></label></th>
                <td>
                    <select id="aisbw-publish-mode" name="publish_mode">
                        <option value="draft_only" <?php selected($topic['publish_mode'], 'draft_only'); ?>>draft_only</option>
                        <option value="autopublish" <?php selected($topic['publish_mode'], 'autopublish'); ?>>autopublish</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-language"><?php esc_html_e('Generation Language', 'guco-ai-blog-writer'); ?></label></th>
                <td>
                    <?php
                    $topic_lang = $topic['language'] ?: get_option('aisbw_default_language', 'pl');
                    $languages = class_exists('AISBW_Multilingual_Premium')
                        ? [
                            'pl' => __('Polish', 'guco-ai-blog-writer'),
                            'en' => __('English', 'guco-ai-blog-writer'),
                            'de' => __('German', 'guco-ai-blog-writer'),
                            'es' => __('Spanish', 'guco-ai-blog-writer'),
                            'fr' => __('French', 'guco-ai-blog-writer'),
                            'it' => __('Italian', 'guco-ai-blog-writer'),
                        ]
                        : [
                            'pl' => __('Polish', 'guco-ai-blog-writer'),
                        ];
                    ?>
                    <select id="aisbw-language" name="language">
                        <?php foreach ($languages as $code => $label) : ?>
                            <option value="<?php echo esc_attr($code); ?>" <?php selected($topic_lang, $code); ?>><?php echo esc_html($label); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <p class="description"><?php esc_html_e('Language in which AI will prepare the article.', 'guco-ai-blog-writer'); ?></p>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-status"><?php esc_html_e('Status', 'guco-ai-blog-writer'); ?></label></th>
                <td>
                    <select id="aisbw-status" name="status">
                        <?php foreach (['idea', 'approved', 'brief_ready', 'draft_ready', 'review_required', 'rejected', 'failed'] as $status) : ?>
                            <option value="<?php echo esc_attr($status); ?>" <?php selected($topic['status'], $status); ?>><?php echo esc_html($this->format_status($status)); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="aisbw-notes"><?php esc_html_e('Editorial Notes', 'guco-ai-blog-writer'); ?></label></th>
                <td><textarea id="aisbw-notes" class="large-text" name="notes" rows="4"><?php echo esc_textarea((string) $topic['notes']); ?></textarea></td>
            </tr>
            <?php if (!empty($topic['brief'])) : ?>
                <?php 
                $brief_json = json_decode((string) $topic['brief'], true);
                $brief_formatted = is_array($brief_json) ? json_encode($brief_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : $topic['brief'];
                ?>
                <tr>
                    <th scope="row"><label for="aisbw-brief"><?php esc_html_e('Generated Brief (JSON)', 'guco-ai-blog-writer'); ?></label></th>
                    <td>
                        <textarea id="aisbw-brief" class="large-text code" name="brief" rows="12" style="font-family: monospace; font-size: 13px;"><?php echo esc_textarea($brief_formatted); ?></textarea>
                        <p class="description"><?php esc_html_e('You can edit the structure and parameters of the brief. Make sure you keep a valid JSON format.', 'guco-ai-blog-writer'); ?></p>
                    </td>
                </tr>
            <?php endif; ?>
        </table>

        <?php submit_button($topic_id ? __('Save Topic', 'guco-ai-blog-writer') : __('Add Topic', 'guco-ai-blog-writer')); ?>
    </form>
</div>
