<?php

namespace AtlasAiDev\Awfah\Admin;

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

/**
 * Dismissible admin notice encouraging users to upgrade to Pro.
 *
 * Shown only to administrators, only when Pro is not active, and only if
 * the user hasn't already dismissed it in the last 30 days.
 */
class Pro_Upsell_Notice {

    /**
     * User meta key used to track dismissal.
     */
    const META_KEY = 'awfah_pro_notice_dismissed';

    /**
     * Days before the notice can reappear after dismissal.
     */
    const DISMISS_DAYS = 30;

    public function __construct() {
        add_action( 'admin_notices', [ $this, 'maybe_show_notice' ] );
        add_action( 'wp_ajax_awfah_dismiss_pro_notice', [ $this, 'ajax_dismiss' ] );
    }

    /**
     * Show the Pro upsell notice if conditions are met.
     */
    public function maybe_show_notice(): void {
        // Only show to admins.
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Don't show if Pro is already active.
        if ( awfah_is_pro_active() ) {
            return;
        }

        // Don't show if recently dismissed.
        $dismissed_at = (int) get_user_meta( get_current_user_id(), self::META_KEY, true );
        if ( $dismissed_at && ( time() - $dismissed_at ) < ( self::DISMISS_DAYS * DAY_IN_SECONDS ) ) {
            return;
        }

        // Only show on our plugin pages.
        $screen = get_current_screen();
        if ( ! $screen || false === strpos( $screen->id, 'ai-workflow-automation-ai-agent-hub' ) ) {
            return;
        }

        ?>
        <div class="notice notice-info is-dismissible awfah-pro-upsell-notice" id="awfah-pro-notice">
            <p>
                <strong>🚀 Unlock the full power of AI Agent Hub</strong> —
                Get unlimited workflows, advanced RBAC, T/R/P access modes, AI experiment pro features, workflow prompt builders, and more with
                <a href="https://atlasaidev.com/ai-agent-hub-pro/" target="_blank" rel="noopener noreferrer"><strong>AI Agent Hub Pro</strong></a>.
            </p>
        </div>
        <script>
        (function() {
            var el = document.getElementById('awfah-pro-notice');
            if (!el) return;
            el.addEventListener('click', function(e) {
                if (e.target.classList.contains('notice-dismiss') || e.target.closest('.notice-dismiss')) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', ajaxurl);
                    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xhr.send('action=awfah_dismiss_pro_notice&_wpnonce=<?php echo esc_js( wp_create_nonce( 'awfah_dismiss_pro_notice' ) ); ?>');
                }
            });
        })();
        </script>
        <?php
    }

    /**
     * AJAX handler: dismiss the notice for the current user.
     */
    public function ajax_dismiss(): void {
        check_ajax_referer( 'awfah_dismiss_pro_notice' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error();
        }

        update_user_meta( get_current_user_id(), self::META_KEY, time() );
        wp_send_json_success();
    }
}
