<?php
if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="wrap aisbw-admin">
    <h1><?php esc_html_e('Generated Drafts', 'guco-ai-blog-writer'); ?></h1>

    <table class="wp-list-table widefat fixed striped aisbw-table">
        <thead>
            <tr>
                <th><?php esc_html_e('ID', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Title', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Status', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Quality Score', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('WP Post ID', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Updated At', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Actions', 'guco-ai-blog-writer'); ?></th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($topics)) : ?>
            <tr>
                <td colspan="7"><?php esc_html_e('No generated drafts found.', 'guco-ai-blog-writer'); ?></td>
            </tr>
        <?php endif; ?>

        <?php foreach ($topics as $topic) : ?>
            <?php
            $topic_id = (int) $topic['id'];
            $article = $this->decode_json($topic['generated_content'] ?? '');
            $post_id = !empty($topic['wp_post_id']) ? (int) $topic['wp_post_id'] : 0;
            ?>
            <tr>
                <td><?php echo esc_html((string) $topic_id); ?></td>
                <td><?php echo esc_html((string) ($article['title'] ?? $topic['working_title'] ?? $topic['topic'])); ?></td>
                <td><span class="aisbw-status aisbw-status-<?php echo esc_attr((string) $topic['status']); ?>"><?php echo esc_html($this->format_status((string) $topic['status'])); ?></span></td>
                <td><?php echo '' !== (string) $topic['quality_score'] ? esc_html((string) $topic['quality_score']) : '-'; ?></td>
                <td>
                    <?php if ($post_id) : ?>
                        <a href="<?php echo esc_url(get_edit_post_link($post_id)); ?>">#<?php echo esc_html((string) $post_id); ?></a>
                    <?php else : ?>
                        -
                    <?php endif; ?>
                </td>
                <td><?php echo esc_html((string) $topic['updated_at']); ?></td>
                <td class="aisbw-actions">
                    <a href="<?php echo esc_url($this->page_url('aisbw-drafts', ['topic_id' => $topic_id])); ?>"><?php esc_html_e('Preview', 'guco-ai-blog-writer'); ?></a>
                    <a href="<?php echo esc_url($this->topic_action_url('regenerate', $topic_id, 'aisbw-drafts')); ?>"><?php esc_html_e('Regenerate Text', 'guco-ai-blog-writer'); ?></a>
                    <a 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>
                    <a 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) : ?>
                        <a 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; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
