<?php
// phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals
 defined( 'ABSPATH' ) || exit;
global $wpdb;
$table     = $wpdb->prefix . 'zs_seo_redirects';
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$redirects = $wpdb->get_results( "SELECT * FROM {$table} ORDER BY created_at DESC" );
?>

<div class="wrap zs-seo-wrap">
    <div class="zs-seo-header">
        <h1>🔀 <?php esc_html_e('Redirects Manager', 'zs-seo-audit-optimization'); ?></h1>
    </div>

    <!-- Add Redirect Form -->
    <div class="zs-seo-box">
        <h2><?php esc_html_e('Add New Redirect', 'zs-seo-audit-optimization'); ?></h2>
        <div class="zs-seo-redirect-form">
            <div class="zs-seo-row">
                <input type="text" id="zs-seo-redirect-source" class="regular-text"
                       placeholder="/old-url-slug" />
                <span style="padding:0 8px;">→</span>
                <input type="text" id="zs-seo-redirect-target" class="regular-text"
                       placeholder="/new-url-slug or https://example.com" />
                <select id="zs-seo-redirect-type">
                    <option value="301">301 (Permanent)</option>
                    <option value="302">302 (Temporary)</option>
                </select>
                <button type="button" id="zs-seo-save-redirect" class="button button-primary">
                    + <?php esc_html_e('Add Redirect', 'zs-seo-audit-optimization'); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- Redirects Table -->
    <div class="zs-seo-box">
        <h2><?php esc_html_e('Active Redirects', 'zs-seo-audit-optimization'); ?></h2>
        <div id="zs-seo-redirects-table">
            <?php if ( empty( $redirects ) ) : ?>
                <p><?php esc_html_e('No redirects yet. Add your first one above.', 'zs-seo-audit-optimization'); ?></p>
            <?php else : ?>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Source URL', 'zs-seo-audit-optimization'); ?></th>
                        <th><?php esc_html_e('Target URL', 'zs-seo-audit-optimization'); ?></th>
                        <th><?php esc_html_e('Type', 'zs-seo-audit-optimization'); ?></th>
                        <th><?php esc_html_e('Hits', 'zs-seo-audit-optimization'); ?></th>
                        <th><?php esc_html_e('Actions', 'zs-seo-audit-optimization'); ?></th>
                    </tr>
                </thead>
                <tbody id="zs-seo-redirects-body">
                    <?php foreach ( $redirects as $r ) : ?>
                    <tr data-id="<?php echo esc_attr( $r->id ); ?>">
                        <td><code><?php echo esc_html( $r->source_url ); ?></code></td>
                        <td><code><?php echo esc_html( $r->target_url ); ?></code></td>
                        <td><span class="zs-seo-badge zs-seo-badge-<?php echo esc_attr( $r->type ); ?>"><?php echo esc_html( $r->type ); ?></span></td>
                        <td><?php echo esc_html( $r->hits ); ?></td>
                        <td>
                            <button type="button" class="button zs-seo-delete-redirect" data-id="<?php echo esc_attr( $r->id ); ?>">
                                🗑 <?php esc_html_e('Delete', 'zs-seo-audit-optimization'); ?>
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
</div>
