<?php
/**
 * Internal Linking Admin Page
 * 
 * @package ContentPerformanceAnalyzer
 * @since 2.0.0
 */

if (!defined('ABSPATH')) {
    exit;
}

// Get published posts and pages for dropdowns
$cpanalyzer_published_posts = get_posts(array(
    'post_type' => array('post', 'page'),
    'post_status' => 'publish',
    'numberposts' => -1,
    'orderby' => 'title',
    'order' => 'ASC'
));
?>

<div class="wrap">
    <h1><?php esc_html_e('Internal Linking (Anti-Hallucination)', 'content-performance-analyzer'); ?></h1>
    
    <div class="notice notice-info">
        <p>
            <strong><?php esc_html_e('How it works:', 'content-performance-analyzer'); ?></strong>
            <?php esc_html_e('This tool uses an extractive-only approach that prevents AI hallucinations. It only suggests links using text that actually exists in your content, with precise character validation.', 'content-performance-analyzer'); ?>
        </p>
    </div>

    <div id="cpanalyzer-interlinking-app">
        <div class="cpanalyzer-interlinking-controls">
            <table class="form-table">
                <tr>
                    <th scope="row">
                        <label for="cpanalyzer_source"><?php esc_html_e('Source Post/Page', 'content-performance-analyzer'); ?></label>
                    </th>
                    <td>
                        <select id="cpanalyzer_source" name="cpanalyzer_source" style="width: 400px;">
                            <option value=""><?php esc_html_e('— Select source (where links will be added) —', 'content-performance-analyzer'); ?></option>
                            <?php foreach ($cpanalyzer_published_posts as $post): ?>
                                <option value="<?php echo esc_attr($post->ID); ?>" data-url="<?php echo esc_attr(get_permalink($post->ID)); ?>">
                                    <?php echo esc_html($post->post_title); ?> (<?php echo esc_html($post->post_type); ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <p class="description"><?php esc_html_e('Choose the post where internal links will be inserted.', 'content-performance-analyzer'); ?></p>
                    </td>
                </tr>
                
                <tr>
                    <th scope="row">
                        <label for="cpanalyzer_target"><?php esc_html_e('Target Post/Page', 'content-performance-analyzer'); ?></label>
                    </th>
                    <td>
                        <select id="cpanalyzer_target" name="cpanalyzer_target" style="width: 400px;">
                            <option value=""><?php esc_html_e('— Select target (what to link to) —', 'content-performance-analyzer'); ?></option>
                            <?php foreach ($cpanalyzer_published_posts as $post): ?>
                                <option value="<?php echo esc_attr($post->ID); ?>" data-url="<?php echo esc_attr(get_permalink($post->ID)); ?>">
                                    <?php echo esc_html($post->post_title); ?> (<?php echo esc_html($post->post_type); ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <p class="description"><?php esc_html_e('Choose the post to link to.', 'content-performance-analyzer'); ?></p>
                    </td>
                </tr>
            </table>
            
            <p class="submit">
                <button type="button" class="button button-primary" id="cpanalyzer_fetch_suggestions">
                    <?php esc_html_e('Get Link Suggestions', 'content-performance-analyzer'); ?>
                </button>
                <span class="spinner" id="cpanalyzer_fetch_spinner"></span>
            </p>
        </div>

        <div id="cpanalyzer_results_container" style="display: none;">
            <h2><?php esc_html_e('Link Suggestions', 'content-performance-analyzer'); ?></h2>
            
            <div class="notice notice-success" style="display: none;" id="cpanalyzer_validation_notice">
                <p><?php esc_html_e('✅ All suggestions have been validated against the actual content to prevent hallucinations.', 'content-performance-analyzer'); ?></p>
            </div>
            
            <div id="cpanalyzer_results"></div>
            
            <div id="cpanalyzer_apply_section" style="display: none;">
                <p class="submit">
                    <button type="button" class="button button-primary" id="cpanalyzer_apply_selected">
                        <?php esc_html_e('Apply Selected Links', 'content-performance-analyzer'); ?>
                    </button>
                    <span class="spinner" id="cpanalyzer_apply_spinner"></span>
                </p>
                
                <p class="description">
                    <?php esc_html_e('Selected links will be inserted into your content immediately without page reload.', 'content-performance-analyzer'); ?>
                </p>
            </div>
        </div>

        <div id="cpanalyzer_messages"></div>
    </div>
</div>
