<?php
if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="wrap aisbw-admin">
    <h1 class="wp-heading-inline"><?php esc_html_e('Gu&Co AI Blog Writer - Topics', 'guco-ai-blog-writer'); ?></h1>
    <a class="page-title-action" href="<?php echo esc_url($this->page_url('aisbw-topic-edit')); ?>"><?php esc_html_e('Add Topic', 'guco-ai-blog-writer'); ?></a>
    <a class="page-title-action" href="<?php echo esc_url($this->page_url('aisbw-import')); ?>"><?php esc_html_e('Import CSV', 'guco-ai-blog-writer'); ?></a>

    <hr class="wp-header-end">

    <table class="wp-list-table widefat fixed striped aisbw-table">
        <thead>
            <tr>
                <th class="column-id"><?php esc_html_e('ID', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Topic', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Słowo kluczowe', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Priority', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Status', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Tryb', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Ryzyko', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('WP Post ID', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Utworzono', '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="10"><?php esc_html_e('No topics found.', 'guco-ai-blog-writer'); ?></td>
            </tr>
        <?php endif; ?>

        <?php foreach ($topics as $topic) : ?>
            <?php
            $topic_id = (int) $topic['id'];
            $post_id = !empty($topic['wp_post_id']) ? (int) $topic['wp_post_id'] : 0;
            ?>
            <tr>
                <td><?php echo esc_html((string) $topic_id); ?></td>
                <td>
                    <strong><?php echo esc_html((string) $topic['topic']); ?></strong>
                    <?php if (!empty($topic['last_error'])) : ?>
                        <p class="aisbw-error-text"><?php echo esc_html((string) $topic['last_error']); ?></p>
                    <?php endif; ?>
                </td>
                <td><?php echo esc_html((string) $topic['primary_keyword']); ?></td>
                <td><?php echo esc_html((string) $topic['priority']); ?></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 esc_html((string) $topic['publish_mode']); ?></td>
                <td><?php echo esc_html((string) $topic['risk_level']); ?></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['created_at']); ?></td>
                <td class="aisbw-actions">
                    <a href="<?php echo esc_url($this->page_url('aisbw-topic-edit', ['topic_id' => $topic_id])); ?>"><?php esc_html_e('Edit', 'guco-ai-blog-writer'); ?></a>
                    <a href="<?php echo esc_url($this->topic_action_url('approve', $topic_id)); ?>"><?php esc_html_e('Approve', 'guco-ai-blog-writer'); ?></a>
                    <a href="<?php echo esc_url($this->topic_action_url('generate_brief', $topic_id)); ?>"><?php esc_html_e('Brief', 'guco-ai-blog-writer'); ?></a>
                    <a href="<?php echo esc_url($this->topic_action_url('generate_article', $topic_id)); ?>"><?php esc_html_e('Article', 'guco-ai-blog-writer'); ?></a>
                    <?php if (!empty($topic['generated_content'])) : ?>
                        <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('create_wp_draft', $topic_id, 'aisbw-drafts')); ?>"><?php esc_html_e('WP Draft', 'guco-ai-blog-writer'); ?></a>
                    <?php endif; ?>
                    <a href="<?php echo esc_url($this->topic_action_url('reject', $topic_id)); ?>"><?php esc_html_e('Reject', 'guco-ai-blog-writer'); ?></a>
                    <a class="aisbw-confirm-delete" href="<?php echo esc_url($this->topic_action_url('delete', $topic_id)); ?>"><?php esc_html_e('Delete', 'guco-ai-blog-writer'); ?></a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
