<?php

/**
 * Leads list admin page (WP Admin → Site Audit → Leads).
 *
 * Features: paginated table, CSV export, CRM sync status badge.
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class Noon_Audit_Admin_Leads {
    public static function register_menu() {
        add_submenu_page(
            'noonelite-audit-engine',
            __( 'Leads', 'noonelite-audit-engine' ),
            __( 'Leads', 'noonelite-audit-engine' ),
            'manage_options',
            'noonelite-audit-engine-leads',
            [__CLASS__, 'render_page']
        );
        add_action( 'admin_enqueue_scripts', [__CLASS__, 'enqueue_assets'] );
    }

    /**
     * Enqueue leads page JS.
     */
    public static function enqueue_assets( $hook ) {
        if ( 'site-audit_page_noonelite-audit-engine-leads' !== $hook ) {
            return;
        }
    }

    // ─── Action Handler ──────────────────────────────────────────────────────
    public static function handle_actions() {
        if ( !isset( $_GET['page'] ) || $_GET['page'] !== 'noonelite-audit-engine-leads' ) {
            return;
        }
    }

    public static function render_page() {
        // phpcs:disable WordPress.Security.NonceVerification.Recommended
        $page = max( 1, (int) (( isset( $_GET['paged'] ) ? sanitize_text_field( wp_unslash( $_GET['paged'] ) ) : 1 )) );
        // phpcs:enable
        $result = Noon_Audit_Database::get_leads( [
            'page'     => $page,
            'per_page' => 20,
        ] );
        $items = $result['items'];
        $total = $result['total'];
        $pages = $result['pages'];
        // Build audit lookup.
        $audits = Noon_Audit_Database::get_all_audits();
        $audit_map = [];
        foreach ( $audits as $a ) {
            $audit_map[$a->id] = $a->title;
        }
        ?>
        <div class="wrap">
            <h1 class="wp-heading-inline"><?php 
        esc_html_e( 'Captured Leads', 'noonelite-audit-engine' );
        ?> <span class="title-count">(<?php 
        echo esc_html( $total );
        ?>)</span></h1>
            <?php 
        ?>
            <hr class="wp-header-end">

            <?php 
        ?>

            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php 
        esc_html_e( 'Email', 'noonelite-audit-engine' );
        ?></th>
                        <th><?php 
        esc_html_e( 'Name', 'noonelite-audit-engine' );
        ?></th>
                        <th><?php 
        esc_html_e( 'Company', 'noonelite-audit-engine' );
        ?></th>
                        <th><?php 
        esc_html_e( 'Audit', 'noonelite-audit-engine' );
        ?></th>
                        <th><?php 
        esc_html_e( 'Score', 'noonelite-audit-engine' );
        ?></th>
                        <th><?php 
        esc_html_e( 'Risk Band', 'noonelite-audit-engine' );
        ?></th>
                        <th><?php 
        esc_html_e( 'Date', 'noonelite-audit-engine' );
        ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
        if ( empty( $items ) ) {
            ?>
                        <tr><td colspan="7"><?php 
            esc_html_e( 'No leads captured yet.', 'noonelite-audit-engine' );
            ?></td></tr>
                    <?php 
        } else {
            ?>
                        <?php 
            foreach ( $items as $lead ) {
                ?>
                            <tr>
                                <td>
                                    <a href="mailto:<?php 
                echo esc_attr( $lead->email );
                ?>" style="font-weight: 600; text-decoration: none;"><?php 
                echo esc_html( $lead->email );
                ?></a><br>
                                    <?php 
                if ( !empty( $lead->marketing_optin ) ) {
                    ?>
                                        <span title="<?php 
                    esc_attr_e( 'Opted in for marketing materials', 'noonelite-audit-engine' );
                    ?>" style="font-size: 10px; background: #dcfce7; color: #166534; padding: 2px 6px; border-radius: 4px; display: inline-block; margin-top: 4px; border: 1px solid #bbf7d0;"><?php 
                    esc_html_e( 'Marketing ✓', 'noonelite-audit-engine' );
                    ?></span>
                                    <?php 
                } else {
                    ?>
                                        <span title="<?php 
                    esc_attr_e( 'Did not opt in for marketing', 'noonelite-audit-engine' );
                    ?>" style="font-size: 10px; background: #f3f4f6; color: #4b5563; padding: 2px 6px; border-radius: 4px; display: inline-block; margin-top: 4px; border: 1px solid #e5e7eb;"><?php 
                    esc_html_e( 'Marketing ✗', 'noonelite-audit-engine' );
                    ?></span>
                                    <?php 
                }
                ?>
                                </td>
                                <td><?php 
                echo esc_html( $lead->name );
                ?></td>
                                <td><?php 
                echo esc_html( $lead->company );
                ?></td>
                                <td><?php 
                echo esc_html( $audit_map[$lead->audit_id] ?? '—' );
                ?></td>
                                <td><strong><?php 
                echo esc_html( $lead->score );
                ?>/100</strong></td>
                                <td>
                                    <?php 
                $band_color = [
                    'critical' => '#ef4444',
                    'high'     => '#f97316',
                    'moderate' => '#eab308',
                    'low'      => '#10b981',
                ];
                $color = $band_color[$lead->risk_band] ?? '#6b7280';
                ?>
                                    <span style="color: <?php 
                echo esc_attr( $color );
                ?>; font-weight: 600;">
                                        <?php 
                echo esc_html( ucfirst( $lead->risk_band ) );
                ?>
                                    </span>
                                </td>
                                <td><?php 
                echo esc_html( $lead->created_at );
                ?></td>
                            </tr>
                        <?php 
            }
            ?>
                    <?php 
        }
        ?>
                </tbody>
            </table>

            <?php 
        if ( $pages > 1 ) {
            ?>
                <div class="tablenav bottom">
                    <div class="tablenav-pages">
                        <?php 
            for ($i = 1; $i <= $pages; $i++) {
                ?>
                            <?php 
                if ( $i === $page ) {
                    ?>
                                <span class="tablenav-pages-navspan button disabled"><?php 
                    echo esc_html( $i );
                    ?></span>
                            <?php 
                } else {
                    ?>
                                <a class="button" href="<?php 
                    echo esc_url( add_query_arg( 'paged', $i ) );
                    ?>"><?php 
                    echo esc_html( $i );
                    ?></a>
                            <?php 
                }
                ?>
                        <?php 
            }
            ?>
                    </div>
                </div>
            <?php 
        }
        ?>
        </div>
        <?php 
    }

}
