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

if (!$topic) {
    ?>
    <div class="wrap aisbw-admin">
        <h1><?php esc_html_e('Post Preview', 'guco-ai-blog-writer'); ?></h1>
        <p><?php esc_html_e('Topic not found.', 'guco-ai-blog-writer'); ?></p>
    </div>
    <?php
    return;
}

$topic_id = (int) $topic['id'];
$article = $this->decode_json($topic['generated_content'] ?? '');
$quality = $this->decode_json($topic['quality_result'] ?? '');
$brief = $this->decode_json($topic['brief'] ?? '');
$post_id = !empty($topic['wp_post_id']) ? (int) $topic['wp_post_id'] : 0;
?>
<div class="wrap aisbw-admin">
    <h1><?php esc_html_e('Preview of Generated Post', 'guco-ai-blog-writer'); ?></h1>

    <p class="aisbw-toolbar">
        <a class="button" href="<?php echo esc_url($this->page_url('aisbw-drafts')); ?>"><?php esc_html_e('Back to Drafts', 'guco-ai-blog-writer'); ?></a>
        <a class="button" href="<?php echo esc_url($this->topic_action_url('regenerate', $topic_id, 'aisbw-drafts')); ?>"><?php esc_html_e('Regenerate Article', 'guco-ai-blog-writer'); ?></a>
        <?php if (!empty($article)) : ?>
            <a class="button" href="<?php echo esc_url($this->topic_action_url('regenerate_featured_image', $topic_id, 'aisbw-drafts')); ?>"><?php esc_html_e('Regenerate Image Only', 'guco-ai-blog-writer'); ?></a>
        <?php endif; ?>
        <a class="button" href="<?php echo esc_url($this->topic_action_url('quality_check', $topic_id, 'aisbw-drafts')); ?>"><?php esc_html_e('Quality check', 'guco-ai-blog-writer'); ?></a>
        <?php if (!$post_id && !empty($article)) : ?>
            <a class="button button-primary" href="<?php echo esc_url($this->topic_action_url('create_wp_draft', $topic_id, 'aisbw-drafts')); ?>"><?php esc_html_e('Create WP Draft', 'guco-ai-blog-writer'); ?></a>
        <?php endif; ?>
        <?php if ($post_id) : ?>
            <a class="button button-primary" href="<?php echo esc_url(get_edit_post_link($post_id)); ?>"><?php echo sprintf(esc_html__('Edit WP Post #%s', 'guco-ai-blog-writer'), esc_html((string) $post_id)); ?></a>
        <?php endif; ?>
    </p>

    <?php if (empty($article)) : ?>
        <div class="notice notice-warning inline"><p><?php esc_html_e('This topic does not have a generated article yet.', 'guco-ai-blog-writer'); ?></p></div>
    <?php else : ?>
        <div class="aisbw-preview-grid">
            <section class="aisbw-panel">
                <h2><?php esc_html_e('Post Data', 'guco-ai-blog-writer'); ?></h2>
                <dl class="aisbw-meta-list">
                    <dt><?php esc_html_e('Featured Image', 'guco-ai-blog-writer'); ?></dt>
                    <dd>
                        <?php if ($post_id && has_post_thumbnail($post_id)) : ?>
                            <div style="margin-bottom: 8px;">
                                <?php echo get_the_post_thumbnail($post_id, 'medium', ['style' => 'max-width: 100%; height: auto; border-radius: 6px; border: 1px solid #c3c4c7;']); ?>
                            </div>
                        <?php else : ?>
                            <span style="color: #666; font-style: italic;"><?php esc_html_e('No image attached yet.', 'guco-ai-blog-writer'); ?></span>
                        <?php endif; ?>
                        <br>
                        <a class="button button-small" style="margin-top: 4px;" href="<?php echo esc_url($this->topic_action_url('regenerate_featured_image', $topic_id, 'aisbw-drafts')); ?>">
                            🔄 <?php esc_html_e('Regenerate Image', 'guco-ai-blog-writer'); ?>
                        </a>
                    </dd>
                    <dt><?php esc_html_e('Title', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($article['title'] ?? '')); ?></dd>
                    <dt><?php esc_html_e('Slug', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($article['slug'] ?? '')); ?></dd>
                    <dt><?php esc_html_e('Excerpt', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($article['excerpt'] ?? '')); ?></dd>
                    <dt><?php esc_html_e('Meta Title', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($article['meta_title'] ?? '')); ?></dd>
                    <dt><?php esc_html_e('Meta Description', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($article['meta_description'] ?? '')); ?></dd>
                    <dt><?php esc_html_e('Tags', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html(implode(', ', (array) ($article['tags'] ?? []))); ?></dd>
                    <dt><?php esc_html_e('Topic Status', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html($this->format_status((string) $topic['status'])); ?></dd>
                </dl>
            </section>

            <section class="aisbw-panel">
                <h2><?php esc_html_e('Quality check', 'guco-ai-blog-writer'); ?></h2>
                <dl class="aisbw-meta-list">
                    <dt><?php esc_html_e('Quality Score', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($quality['quality_score'] ?? $topic['quality_score'] ?? '-')); ?></dd>
                    <dt><?php esc_html_e('Risk Level', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($quality['risk_level'] ?? $topic['risk_level'] ?? '-')); ?></dd>
                    <dt><?php esc_html_e('Human Review', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo !empty($quality['requires_human_review']) ? esc_html__('yes', 'guco-ai-blog-writer') : esc_html__('no', 'guco-ai-blog-writer'); ?></dd>
                    <dt><?php esc_html_e('Recommendation', 'guco-ai-blog-writer'); ?></dt>
                    <dd><?php echo esc_html((string) ($quality['recommended_action'] ?? '-')); ?></dd>
                </dl>
                <?php if (!empty($quality['issues']) && is_array($quality['issues'])) : ?>
                    <h3><?php esc_html_e('Issues', 'guco-ai-blog-writer'); ?></h3>
                    <ul class="aisbw-issues">
                        <?php foreach ($quality['issues'] as $issue) : ?>
                            <li>
                                <strong><?php echo esc_html((string) ($issue['severity'] ?? '')); ?></strong>
                                <?php echo esc_html((string) ($issue['type'] ?? '')); ?>:
                                <?php echo esc_html((string) ($issue['message'] ?? '')); ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </section>
        </div>

        <?php if (!empty($brief)) : ?>
            <section class="aisbw-panel">
                <h2><?php esc_html_e('Brief', 'guco-ai-blog-writer'); ?></h2>
                <pre class="aisbw-code"><?php echo esc_html(wp_json_encode($brief, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); ?></pre>
            </section>
        <?php endif; ?>

        <section class="aisbw-panel aisbw-content-preview">
            <h2><?php esc_html_e('HTML Content', 'guco-ai-blog-writer'); ?></h2>
            <div class="aisbw-generated-content">
                <?php echo wp_kses_post((string) ($article['content_html'] ?? '')); ?>
            </div>
        </section>
    <?php endif; ?>
</div>
