<?php
/**
 * Admin Dashboard
 *
 * Handles the plugin's admin pages: dashboard, settings, tools.
 */

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

class Tiriri_Admin {

    /**
     * Initialize admin hooks
     */
    public function init() {
        add_action( 'admin_menu', array( $this, 'add_menu_pages' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
        add_action( 'wp_ajax_tiriri_rescan', array( $this, 'ajax_rescan' ) );
        add_action( 'wp_ajax_tiriri_add_manual', array( $this, 'ajax_add_manual_declaration' ) );
        add_action( 'wp_ajax_tiriri_remove_manual', array( $this, 'ajax_remove_manual_declaration' ) );

        // Show admin notice if non-compliant
        add_action( 'admin_notices', array( $this, 'compliance_notice' ) );

    }

    /**
     * Add menu pages
     */
    public function add_menu_pages() {
        // Main menu item
        add_menu_page(
            __( 'Tiriri', 'tiriri-transparency-for-eu-ai-act' ),
            __( 'Tiriri', 'tiriri-transparency-for-eu-ai-act' ),
            'manage_options',
            'tiriri-transparency-for-eu-ai-act',
            array( $this, 'render_dashboard' ),
            'dashicons-shield',
            80
        );

        // Dashboard (same as main)
        add_submenu_page(
            'tiriri-transparency-for-eu-ai-act',
            __( 'Compliance Dashboard', 'tiriri-transparency-for-eu-ai-act' ),
            __( 'Dashboard', 'tiriri-transparency-for-eu-ai-act' ),
            'manage_options',
            'tiriri-transparency-for-eu-ai-act',
            array( $this, 'render_dashboard' )
        );

        // Settings
        add_submenu_page(
            'tiriri-transparency-for-eu-ai-act',
            __( 'Settings', 'tiriri-transparency-for-eu-ai-act' ),
            __( 'Settings', 'tiriri-transparency-for-eu-ai-act' ),
            'manage_options',
            'tiriri-transparency-for-eu-ai-act-settings',
            array( $this, 'render_settings' )
        );
    }

    /**
     * Enqueue admin assets
     */
    public function enqueue_assets( $hook ) {
        if ( strpos( $hook, 'tiriri-transparency-for-eu-ai-act' ) === false ) {
            return;
        }

        wp_enqueue_style(
            'tiriri-admin',
            TIRIRI_PLUGIN_URL . 'admin/assets/admin.css',
            array(),
            TIRIRI_VERSION
        );

        wp_enqueue_script(
            'tiriri-admin',
            TIRIRI_PLUGIN_URL . 'admin/assets/admin.js',
            array( 'jquery' ),
            TIRIRI_VERSION,
            true
        );

        wp_localize_script( 'tiriri-admin', 'tiririAdmin', array(
            'ajaxUrl' => admin_url( 'admin-ajax.php' ),
            'nonce'   => wp_create_nonce( 'tiriri_admin' ),
            'strings' => array(
                'scanning'     => __( 'Scanning...', 'tiriri-transparency-for-eu-ai-act' ),
                'scanComplete' => __( 'Scan complete!', 'tiriri-transparency-for-eu-ai-act' ),
                'error'        => __( 'An error occurred.', 'tiriri-transparency-for-eu-ai-act' ),
                'confirm'      => __( 'Are you sure?', 'tiriri-transparency-for-eu-ai-act' ),
                'rescan'       => __( 'Re-scan Now', 'tiriri-transparency-for-eu-ai-act' ),
                'removing'     => __( 'Removing...', 'tiriri-transparency-for-eu-ai-act' ),
                'remove'       => __( 'Remove', 'tiriri-transparency-for-eu-ai-act' ),
            ),
        ) );
    }

    /**
     * Render the compliance dashboard
     */
    public function render_dashboard() {
        $scanner = new Tiriri_Scanner();
        $summary = $scanner->get_summary();
        $results = get_option( 'tiriri_scan_results', array() );
        $plugins = isset( $results['plugins'] ) ? $results['plugins'] : array();
        $manual  = isset( $results['manual'] ) ? $results['manual'] : array();

        ?>
        <div class="wrap tiriri-wrap">
            <h1><?php esc_html_e( 'Tiriri — EU AI Act Compliance Dashboard', 'tiriri-transparency-for-eu-ai-act' ); ?></h1>

            <!-- Deadline Banner -->
            <div class="tiriri-deadline-banner">
                <div class="tiriri-deadline-banner__content">
                    <strong><?php esc_html_e( 'EU AI Act Article 50 Enforcement:', 'tiriri-transparency-for-eu-ai-act' ); ?></strong>
                    <?php if ( $summary['days_until_deadline'] > 0 ) : ?>
                        <span class="tiriri-deadline-days">
                            <?php printf(
                                esc_html( _n( '%d day remaining', '%d days remaining', $summary['days_until_deadline'], 'tiriri-transparency-for-eu-ai-act' ) ),
                                intval( $summary['days_until_deadline'] )
                            ); ?>
                        </span>
                        <span class="tiriri-deadline-date">(<?php echo esc_html( date_i18n( 'F j, Y', strtotime( '2026-08-02' ) ) ); ?>)</span>
                    <?php else : ?>
                        <span class="tiriri-deadline-active"><?php esc_html_e( 'NOW IN EFFECT', 'tiriri-transparency-for-eu-ai-act' ); ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Status Cards -->
            <div class="tiriri-cards">
                <!-- Overall Status -->
                <div class="tiriri-card tiriri-card--status tiriri-card--<?php echo esc_attr( $summary['compliance_status'] ); ?>">
                    <h2><?php esc_html_e( 'Compliance Status', 'tiriri-transparency-for-eu-ai-act' ); ?></h2>
                    <div class="tiriri-card__status-indicator">
                        <?php echo wp_kses_post( $this->get_status_html( $summary['compliance_status'] ) ); ?>
                    </div>
                </div>

                <!-- AI Tools Detected -->
                <div class="tiriri-card">
                    <h2><?php esc_html_e( 'AI Tools Detected', 'tiriri-transparency-for-eu-ai-act' ); ?></h2>
                    <div class="tiriri-card__number"><?php echo intval( $summary['total_detected'] ); ?></div>
                    <div class="tiriri-card__breakdown">
                        <span><?php printf( esc_html__( '%d chatbots', 'tiriri-transparency-for-eu-ai-act' ), intval( $summary['chatbots'] ) ); ?></span>
                        <span><?php printf( esc_html__( '%d content generators', 'tiriri-transparency-for-eu-ai-act' ), intval( $summary['content_generators'] ) ); ?></span>
                        <span><?php printf( esc_html__( '%d other', 'tiriri-transparency-for-eu-ai-act' ), intval( $summary['other_ai'] ) ); ?></span>
                    </div>
                </div>

                <!-- Disclosures -->
                <div class="tiriri-card">
                    <h2><?php esc_html_e( 'Disclosures', 'tiriri-transparency-for-eu-ai-act' ); ?></h2>
                    <div class="tiriri-card__number">
                        <?php echo intval( $summary['disclosed'] ); ?> / <?php echo intval( $summary['requiring_disclosure'] ); ?>
                    </div>
                    <div class="tiriri-card__label"><?php esc_html_e( 'Active / Required', 'tiriri-transparency-for-eu-ai-act' ); ?></div>
                </div>
            </div>

            <!-- Detected AI Tools Table -->
            <div class="tiriri-section">
                <div class="tiriri-section__header">
                    <h2><?php esc_html_e( 'Detected AI Tools', 'tiriri-transparency-for-eu-ai-act' ); ?></h2>
                    <button id="tiriri-rescan" class="button button-secondary">
                        <?php esc_html_e( 'Re-scan Now', 'tiriri-transparency-for-eu-ai-act' ); ?>
                    </button>
                </div>

                <?php if ( empty( $plugins ) && empty( $manual ) ) : ?>
                    <div class="tiriri-empty">
                        <p><?php esc_html_e( 'No AI tools detected on your site. If you use AI tools not listed here, you can add them manually below.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>
                    </div>
                <?php else : ?>
                    <table class="wp-list-table widefat fixed striped">
                        <thead>
                            <tr>
                                <th><?php esc_html_e( 'Tool', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                                <th><?php esc_html_e( 'Category', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                                <th><?php esc_html_e( 'Risk Level', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                                <th><?php esc_html_e( 'Article', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                                <th><?php esc_html_e( 'Disclosure Status', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                                <th><?php esc_html_e( 'Action', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ( array_merge( $plugins, $manual ) as $tool ) : ?>
                                <tr>
                                    <td>
                                        <strong><?php echo esc_html( $tool['name'] ); ?></strong>
                                        <br>
                                        <small class="description"><?php echo esc_html( $tool['note'] ?? '' ); ?></small>
                                    </td>
                                    <td><?php echo esc_html( $this->format_category( $tool['category'] ) ); ?></td>
                                    <td><?php echo wp_kses_post( $this->format_risk( $tool['risk'] ) ); ?></td>
                                    <td><?php echo esc_html( $tool['article'] ?? '' ); ?></td>
                                    <td><?php echo wp_kses_post( $this->format_disclosure_status( $tool['disclosure_status'] ?? 'not_configured' ) ); ?></td>
                                    <td>
                                        <?php if ( ( $tool['disclosure_status'] ?? '' ) !== 'active' ) : ?>
                                            <a href="<?php echo esc_url( admin_url( 'admin.php?page=tiriri-transparency-for-eu-ai-act-settings' ) ); ?>" class="button button-small button-primary">
                                                <?php esc_html_e( 'Configure', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                            </a>
                                        <?php else : ?>
                                            <span class="dashicons dashicons-yes-alt" style="color: #27ae60;"></span>
                                        <?php endif; ?>
                                        <?php if ( ( $tool['source'] ?? '' ) === 'manual' && ! empty( $tool['id'] ) ) : ?>
                                            <button type="button" class="button button-small button-link-delete tiriri-remove-manual" data-id="<?php echo esc_attr( $tool['id'] ); ?>">
                                                <?php esc_html_e( 'Remove', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                            </button>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>

            <!-- Manual Declaration -->
            <div class="tiriri-section">
                <h2><?php esc_html_e( 'Manually Declare AI Tools', 'tiriri-transparency-for-eu-ai-act' ); ?></h2>
                <p class="description"><?php esc_html_e( 'If you use AI tools that are not automatically detected (e.g., external AI services, custom integrations), declare them here.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>

                <form id="tiriri-manual-form">
                    <table class="form-table">
                        <tr>
                            <th><label for="manual_name"><?php esc_html_e( 'Tool Name', 'tiriri-transparency-for-eu-ai-act' ); ?></label></th>
                            <td><input type="text" id="manual_name" class="regular-text" required /></td>
                        </tr>
                        <tr>
                            <th><label for="manual_category"><?php esc_html_e( 'Category', 'tiriri-transparency-for-eu-ai-act' ); ?></label></th>
                            <td>
                                <select id="manual_category">
                                    <option value="chatbot"><?php esc_html_e( 'AI Chatbot', 'tiriri-transparency-for-eu-ai-act' ); ?></option>
                                    <option value="content_generation"><?php esc_html_e( 'Content Generation', 'tiriri-transparency-for-eu-ai-act' ); ?></option>
                                    <option value="image_generation"><?php esc_html_e( 'Image Generation', 'tiriri-transparency-for-eu-ai-act' ); ?></option>
                                    <option value="translation_ai"><?php esc_html_e( 'AI Translation', 'tiriri-transparency-for-eu-ai-act' ); ?></option>
                                    <option value="other"><?php esc_html_e( 'Other AI Tool', 'tiriri-transparency-for-eu-ai-act' ); ?></option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="manual_note"><?php esc_html_e( 'Description', 'tiriri-transparency-for-eu-ai-act' ); ?></label></th>
                            <td><input type="text" id="manual_note" class="regular-text" placeholder="<?php esc_attr_e( 'How this tool is used on your site', 'tiriri-transparency-for-eu-ai-act' ); ?>" /></td>
                        </tr>
                    </table>
                    <button type="submit" class="button button-secondary"><?php esc_html_e( 'Add Tool', 'tiriri-transparency-for-eu-ai-act' ); ?></button>
                </form>
            </div>

            <!-- Last Scan Info -->
            <?php if ( ! empty( $summary['last_scan'] ) ) : ?>
                <p class="description" style="margin-top: 20px;">
                    <?php printf( esc_html__( 'Last scan: %s', 'tiriri-transparency-for-eu-ai-act' ), esc_html( $summary['last_scan'] ) ); ?>
                    &bull;
                    <?php esc_html_e( 'Scans run automatically once daily and when plugins are activated/deactivated.', 'tiriri-transparency-for-eu-ai-act' ); ?>
                </p>
            <?php endif; ?>
        </div>
        <?php
    }

    /**
     * Render settings page
     */
    public function render_settings() {
        $settings = get_option( 'tiriri_settings', array() );
        ?>
        <div class="wrap tiriri-wrap">
            <h1><?php esc_html_e( 'Tiriri — Settings', 'tiriri-transparency-for-eu-ai-act' ); ?></h1>

            <?php
            // Show save confirmation on custom admin pages
            if ( ! empty( $_GET['settings-updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                add_settings_error( 'tiriri_settings_group', 'settings_updated', __( 'Settings saved.', 'tiriri-transparency-for-eu-ai-act' ), 'updated' );
            }
            settings_errors( 'tiriri_settings_group' );
            ?>

            <form method="post" action="options.php">
                <?php settings_fields( 'tiriri_settings_group' ); ?>
                <input type="hidden" name="_wp_http_referer" value="<?php echo esc_attr( admin_url( 'admin.php?page=tiriri-transparency-for-eu-ai-act-settings&settings-updated=true' ) ); ?>" />

                <!-- Chatbot Disclosure Settings -->
                <div class="tiriri-settings-section">
                    <h2><?php esc_html_e( 'Chatbot Disclosure Banner', 'tiriri-transparency-for-eu-ai-act' ); ?></h2>
                    <p class="description"><?php esc_html_e( 'Article 50(1): Users must be informed when interacting with an AI system.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>

                    <table class="form-table">
                        <tr>
                            <th><?php esc_html_e( 'Enable Disclosure', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox"
                                           name="tiriri_settings[chatbot_disclosure_enabled]"
                                           value="1"
                                           <?php checked( ! empty( $settings['chatbot_disclosure_enabled'] ) ); ?> />
                                    <?php esc_html_e( 'Show disclosure notice when AI chatbot is detected', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="disclosure_text"><?php esc_html_e( 'Disclosure Text', 'tiriri-transparency-for-eu-ai-act' ); ?></label></th>
                            <td>
                                <input type="text"
                                       id="disclosure_text"
                                       name="tiriri_settings[chatbot_disclosure_text]"
                                       value="<?php echo esc_attr( $settings['chatbot_disclosure_text'] ?? __( 'You are interacting with an AI assistant.', 'tiriri-transparency-for-eu-ai-act' ) ); ?>"
                                       class="large-text" />
                                <p class="description"><?php esc_html_e( 'This text appears when a visitor starts interacting with an AI chatbot.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="disclosure_style"><?php esc_html_e( 'Display Style', 'tiriri-transparency-for-eu-ai-act' ); ?></label></th>
                            <td>
                                <select id="disclosure_style" name="tiriri_settings[chatbot_disclosure_style]">
                                    <option value="banner" <?php selected( $settings['chatbot_disclosure_style'] ?? '', 'banner' ); ?>>
                                        <?php esc_html_e( 'Banner (top of chat)', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                    </option>
                                    <option value="tooltip" <?php selected( $settings['chatbot_disclosure_style'] ?? '', 'tooltip' ); ?>>
                                        <?php esc_html_e( 'Tooltip (near chat icon)', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                    </option>
                                    <option value="inline" <?php selected( $settings['chatbot_disclosure_style'] ?? '', 'inline' ); ?>>
                                        <?php esc_html_e( 'Inline (inside chat window)', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                    </option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>

                <!-- Content Labeling Settings -->
                <div class="tiriri-settings-section">
                    <h2>
                        <?php esc_html_e( 'Content Labeling', 'tiriri-transparency-for-eu-ai-act' ); ?>
                        <?php if ( ! tiriri_is_pro() ) : ?>
                            <span class="tiriri-pro-badge"><?php esc_html_e( 'PRO', 'tiriri-transparency-for-eu-ai-act' ); ?></span>
                        <?php endif; ?>
                    </h2>
                    <p class="description"><?php esc_html_e( 'Article 50(2) & 50(4): AI-generated content must be labeled.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>

                    <?php if ( tiriri_is_pro() ) : ?>
                    <table class="form-table">
                        <tr>
                            <th><?php esc_html_e( 'Enable Labels', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox"
                                           name="tiriri_settings[content_labeling_enabled]"
                                           value="1"
                                           <?php checked( ! empty( $settings['content_labeling_enabled'] ) ); ?> />
                                    <?php esc_html_e( 'Display AI content labels on posts/pages', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                </label>
                            </td>
                        </tr>
                    </table>
                    <?php else : ?>
                    <div class="tiriri-pro-upsell">
                        <p><?php esc_html_e( 'Automatically label AI-generated and AI-assisted content on your posts and pages with visible badges and machine-readable metadata (Schema.org).', 'tiriri-transparency-for-eu-ai-act' ); ?></p>
                        <a href="<?php echo esc_url( tiriri_fs()->get_upgrade_url() ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro', 'tiriri-transparency-for-eu-ai-act' ); ?></a>
                    </div>
                    <?php endif; ?>
                </div>

                <!-- Script Detection Settings -->
                <div class="tiriri-settings-section">
                    <h2>
                        <?php esc_html_e( 'Third-Party Script Detection', 'tiriri-transparency-for-eu-ai-act' ); ?>
                        <?php if ( ! tiriri_is_pro() ) : ?>
                            <span class="tiriri-pro-badge"><?php esc_html_e( 'PRO', 'tiriri-transparency-for-eu-ai-act' ); ?></span>
                        <?php endif; ?>
                    </h2>

                    <?php if ( tiriri_is_pro() ) : ?>
                    <table class="form-table">
                        <tr>
                            <th><?php esc_html_e( 'Scan Scripts', 'tiriri-transparency-for-eu-ai-act' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox"
                                           name="tiriri_settings[scan_third_party_scripts]"
                                           value="1"
                                           <?php checked( ! empty( $settings['scan_third_party_scripts'] ) ); ?> />
                                    <?php esc_html_e( 'Detect third-party AI chatbot scripts (Intercom, Drift, Crisp, etc.)', 'tiriri-transparency-for-eu-ai-act' ); ?>
                                </label>
                                <p class="description"><?php esc_html_e( 'Scans your pages for embedded AI chatbot scripts that are not WordPress plugins.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>
                            </td>
                        </tr>
                    </table>
                    <?php else : ?>
                    <div class="tiriri-pro-upsell">
                        <p><?php esc_html_e( 'Detect AI chatbot scripts from Intercom, Drift, Crisp, Tidio, and 8 other providers — even if they are not installed as WordPress plugins.', 'tiriri-transparency-for-eu-ai-act' ); ?></p>
                        <a href="<?php echo esc_url( tiriri_fs()->get_upgrade_url() ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro', 'tiriri-transparency-for-eu-ai-act' ); ?></a>
                    </div>
                    <?php endif; ?>
                </div>

                <?php submit_button( __( 'Save Settings', 'tiriri-transparency-for-eu-ai-act' ) ); ?>
            </form>
        </div>
        <?php
    }

    /**
     * AJAX handler: Re-scan
     */
    public function ajax_rescan() {
        check_ajax_referer( 'tiriri_admin', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( __( 'Unauthorized', 'tiriri-transparency-for-eu-ai-act' ) );
        }

        $scanner = new Tiriri_Scanner();
        $results = $scanner->run_scan();
        $summary = $scanner->get_summary();

        wp_send_json_success( array(
            'results' => $results,
            'summary' => $summary,
        ) );
    }

    /**
     * AJAX handler: Add manual declaration
     */
    public function ajax_add_manual_declaration() {
        check_ajax_referer( 'tiriri_admin', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( __( 'Unauthorized', 'tiriri-transparency-for-eu-ai-act' ) );
        }

        $declaration = array(
            'name'              => isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '',
            'category'          => isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : 'other',
            'risk'              => 'requires_disclosure',
            'article'           => '50(1)',
            'note'              => isset( $_POST['note'] ) ? sanitize_text_field( wp_unslash( $_POST['note'] ) ) : '',
            'disclosure_status' => 'not_configured',
        );

        if ( empty( $declaration['name'] ) ) {
            wp_send_json_error( __( 'Name is required.', 'tiriri-transparency-for-eu-ai-act' ) );
        }

        $scanner = new Tiriri_Scanner();
        $result = $scanner->add_manual_declaration( $declaration );

        // Re-run scan to update results
        $scanner->run_scan();

        wp_send_json_success( $result );
    }

    /**
     * AJAX handler: Remove manual declaration
     */
    public function ajax_remove_manual_declaration() {
        check_ajax_referer( 'tiriri_admin', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( __( 'Unauthorized', 'tiriri-transparency-for-eu-ai-act' ) );
        }

        $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '';
        if ( empty( $id ) ) {
            wp_send_json_error( __( 'ID is required.', 'tiriri-transparency-for-eu-ai-act' ) );
        }

        $scanner = new Tiriri_Scanner();
        $scanner->remove_manual_declaration( $id );
        $scanner->run_scan();

        wp_send_json_success();
    }

    /**
     * Show admin notice if non-compliant
     */
    public function compliance_notice() {
        // Only on non-plugin pages
        $screen = get_current_screen();
        if ( $screen && strpos( $screen->id, 'tiriri-transparency-for-eu-ai-act' ) !== false ) {
            return;
        }

        $scanner = new Tiriri_Scanner();
        $status = $scanner->get_compliance_status();

        if ( 'non_compliant' !== $status && 'partial' !== $status ) {
            return;
        }

        $days         = $scanner->get_days_until_enforcement();
        $dashboard_url = esc_url( admin_url( 'admin.php?page=tiriri-transparency-for-eu-ai-act' ) );

        if ( $days > 0 ) {
            $link_text = esc_html__( 'View Dashboard', 'tiriri-transparency-for-eu-ai-act' );
            $message   = sprintf(
                /* translators: 1: number of days until enforcement, 2: dashboard link HTML */
                esc_html__( 'AI tools detected on your site require EU AI Act disclosures. %1$d days until enforcement. %2$s', 'tiriri-transparency-for-eu-ai-act' ),
                (int) $days,
                '<a href="' . $dashboard_url . '">' . $link_text . '</a>'
            );
        } else {
            $link_text = esc_html__( 'Configure Now', 'tiriri-transparency-for-eu-ai-act' );
            $message   = sprintf(
                /* translators: %s: dashboard link HTML */
                esc_html__( 'The EU AI Act is now enforceable. AI tools on your site require transparency disclosures. %s', 'tiriri-transparency-for-eu-ai-act' ),
                '<a href="' . $dashboard_url . '">' . $link_text . '</a>'
            );
        }

        $allowed = array(
            'a'      => array( 'href' => array() ),
            'strong' => array(),
        );
        ?>
        <div class="notice notice-warning is-dismissible">
            <p>
                <strong><?php esc_html_e( 'Tiriri:', 'tiriri-transparency-for-eu-ai-act' ); ?></strong>
                <?php echo wp_kses( $message, $allowed ); ?>
            </p>
        </div>
        <?php
    }

    // --- Helper methods ---

    private function get_status_html( $status ) {
        $statuses = array(
            'compliant'        => '<span class="tiriri-status tiriri-status--green">' . esc_html__( 'Compliant', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'partial'          => '<span class="tiriri-status tiriri-status--yellow">' . esc_html__( 'Partially Compliant', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'non_compliant'    => '<span class="tiriri-status tiriri-status--red">' . esc_html__( 'Not Compliant', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'no_ai_detected'   => '<span class="tiriri-status tiriri-status--gray">' . esc_html__( 'No AI Detected', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'no_action_needed' => '<span class="tiriri-status tiriri-status--green">' . esc_html__( 'No Action Needed', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
        );
        return $statuses[ $status ] ?? $statuses['no_ai_detected'];
    }

    private function format_category( $category ) {
        $categories = array(
            'chatbot'            => __( 'AI Chatbot', 'tiriri-transparency-for-eu-ai-act' ),
            'content_generation' => __( 'Content Generation', 'tiriri-transparency-for-eu-ai-act' ),
            'seo_ai'            => __( 'SEO AI', 'tiriri-transparency-for-eu-ai-act' ),
            'image_generation'   => __( 'Image Generation', 'tiriri-transparency-for-eu-ai-act' ),
            'page_builder_ai'   => __( 'Page Builder AI', 'tiriri-transparency-for-eu-ai-act' ),
            'translation_ai'    => __( 'AI Translation', 'tiriri-transparency-for-eu-ai-act' ),
            'ecommerce_ai'      => __( 'E-Commerce AI', 'tiriri-transparency-for-eu-ai-act' ),
            'other'              => __( 'Other', 'tiriri-transparency-for-eu-ai-act' ),
        );
        return $categories[ $category ] ?? $category;
    }

    private function format_risk( $risk ) {
        $risks = array(
            'requires_disclosure'     => '<span style="color:#e74c3c; font-weight:600;">' . esc_html__( 'Disclosure Required', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'requires_labeling'       => '<span style="color:#e74c3c; font-weight:600;">' . esc_html__( 'Labeling Required', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'may_require_disclosure'  => '<span style="color:#e67e22;">' . esc_html__( 'May Require Disclosure', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'may_require_labeling'    => '<span style="color:#e67e22;">' . esc_html__( 'May Require Labeling', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'low_risk'                => '<span style="color:#27ae60;">' . esc_html__( 'Low Risk', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
        );
        return $risks[ $risk ] ?? esc_html( $risk );
    }

    private function format_disclosure_status( $status ) {
        $statuses = array(
            'active'         => '<span style="color:#27ae60; font-weight:600;">' . esc_html__( 'Active', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
            'not_configured' => '<span style="color:#e74c3c;">' . esc_html__( 'Not Configured', 'tiriri-transparency-for-eu-ai-act' ) . '</span>',
        );
        return $statuses[ $status ] ?? esc_html( $status );
    }
}
