<?php
/**
 * Redirects view: list, add form, toggle, delete.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

// Handle "add" submission
if ( ! empty( $_POST['seoforge_redirect_add'] ) && check_admin_referer( 'seoforge_redirect_add' ) ) {
    $src    = isset( $_POST['source_path'] ) ? wp_unslash( $_POST['source_path'] ) : '';
    $tgt    = isset( $_POST['target_url'] ) ? wp_unslash( $_POST['target_url'] ) : '';
    $status = isset( $_POST['status_code'] ) ? (int) $_POST['status_code'] : 301;
    $match  = isset( $_POST['match_type'] ) ? sanitize_key( wp_unslash( $_POST['match_type'] ) ) : 'exact';
    $note   = isset( $_POST['note'] ) ? wp_unslash( $_POST['note'] ) : '';
    if ( $src && $tgt ) {
        Seoforge_Redirects::add( $src, $tgt, $status, $match, $note );
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Redirect added.', 'brainwerk-seo-suite' ) . '</p></div>';
    }
}

$rows = Seoforge_Redirects::all( 200 );
?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Add a redirect', 'brainwerk-seo-suite' ); ?></h2>
    <form method="post" action="">
        <?php wp_nonce_field( 'seoforge_redirect_add' ); ?>
        <input type="hidden" name="seoforge_redirect_add" value="1">
        <table class="form-table">
            <tr>
                <th><?php esc_html_e( 'Source path', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="text" name="source_path" class="regular-text" placeholder="/old-page" required></td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Target URL', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="url" name="target_url" class="regular-text" placeholder="https://example.com/new-page" required></td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Status', 'brainwerk-seo-suite' ); ?></th>
                <td>
                    <select name="status_code">
                        <option value="301">301 — Permanent</option>
                        <option value="302">302 — Temporary</option>
                        <option value="307">307 — Temporary (preserve method)</option>
                        <option value="308">308 — Permanent (preserve method)</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Match', 'brainwerk-seo-suite' ); ?></th>
                <td>
                    <select name="match_type">
                        <option value="exact"><?php esc_html_e( 'Exact match', 'brainwerk-seo-suite' ); ?></option>
                        <option value="prefix"><?php esc_html_e( 'Prefix match', 'brainwerk-seo-suite' ); ?></option>
                    </select>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Note (optional)', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="text" name="note" class="regular-text"></td>
            </tr>
        </table>
        <?php submit_button( __( 'Add redirect', 'brainwerk-seo-suite' ) ); ?>
    </form>
</div>

<div class="seoforge-card">
    <h2><?php esc_html_e( 'Active redirects', 'brainwerk-seo-suite' ); ?> (<?php echo count( $rows ); ?>)</h2>
    <table class="widefat striped">
        <thead>
            <tr>
                <th><?php esc_html_e( 'From', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'To', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Status', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Match', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Hits', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Active', 'brainwerk-seo-suite' ); ?></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
        <?php if ( empty( $rows ) ) : ?>
            <tr><td colspan="7"><?php esc_html_e( 'No redirects yet.', 'brainwerk-seo-suite' ); ?></td></tr>
        <?php else : foreach ( $rows as $r ) : ?>
            <tr>
                <td><code><?php echo esc_html( $r['source_path'] ); ?></code></td>
                <td><a href="<?php echo esc_url( $r['target_url'] ); ?>" target="_blank"><?php echo esc_html( $r['target_url'] ); ?></a></td>
                <td><?php echo esc_html( $r['status_code'] ); ?></td>
                <td><?php echo esc_html( $r['match_type'] ); ?></td>
                <td><?php echo esc_html( number_format_i18n( $r['hits'] ) ); ?></td>
                <td>
                    <button type="button" class="button button-small sf-toggle-redir" data-id="<?php echo (int) $r['id']; ?>"
                        aria-label="<?php echo esc_attr( sprintf( /* translators: %s: redirect source path */ __( 'Enable or disable the redirect for %s', 'brainwerk-seo-suite' ), $r['source_path'] ) ); ?>">
                        <?php echo $r['is_active'] ? esc_html__( 'On', 'brainwerk-seo-suite' ) : esc_html__( 'Off', 'brainwerk-seo-suite' ); ?>
                    </button>
                </td>
                <td>
                    <button type="button" class="button button-small sf-delete-redir" data-id="<?php echo (int) $r['id']; ?>"
                        aria-label="<?php echo esc_attr( sprintf( /* translators: %s: redirect source path */ __( 'Delete the redirect for %s', 'brainwerk-seo-suite' ), $r['source_path'] ) ); ?>">
                        <?php esc_html_e( 'Delete', 'brainwerk-seo-suite' ); ?>
                    </button>
                </td>
            </tr>
        <?php endforeach; endif; ?>
        </tbody>
    </table>
</div>
