<?php

namespace Fab\Controller;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Interfaces\JIT_Interface;
use Fab\Controller\Notice;
use Fab\View;

/**
 * DataAgent Cross Promotion Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller/Backend/Notice/Cross_Promote
 */
class DataAgent_Notice extends Notice implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        $this->id                         = 'fab_cross_promote_dataagent';
        $this->non_dismissible_by_screens = array(
            'fab_page_floating-awesome-button-setting',
            'fab_page_floating-awesome-button-templates',
            'fab_page_fab-dashboard',
        );
    }

    /**
     * Show admin DataAgent promotion notice.
     *
     * @return void
     */
    public function fab_show_admin_notice() {
        // Get the current screen.
        $screen = get_current_screen();

        // Check if DataAgent is already active.
        if ( FAB_DATAAGENT_PLUGIN_ACTIVE ) {
            return;
        }

        // Check if the notice should be shown.
        if ( ! $this->should_show_notice() ) {
            return;
        }

        // Setup Buttons.
        $buttons   = array();
        $buttons[] = array(
            'message' => __( 'Install DataAgent', 'floating-awesome-button' ),
            'classes' => 'text-white text-center text-xs px-2.5 py-1.5 bg-primary-600 hover:bg-primary-700 transition-all font-semibold shadow-md animate__animated animate__pulse animate__infinite animate__slow',
            'url'     => admin_url( 'plugin-install.php?tab=plugin-information&plugin=dataagent' ),
        );
        if ( ! in_array( $screen->id, $this->non_dismissible_by_screens, true ) ) {
            $buttons[] = array(
                'message' => __( 'Maybe later', 'floating-awesome-button' ),
                'action'  => 'fab_dismiss_cross_promote_dataagent',
                'classes' => 'text-center text-xs px-2.5 py-1.5 bg-white hover:bg-red-600 border border-primary-600 hover:border-red-600 hover:text-red-600 hover:text-white transition-all font-semibold',
            );
        }

        // Prepare the data for the admin notice.
        $data = array_merge(
            $this->get_default_data(),
            array(
                'title'   => __( 'Supercharge Your Site with DataAgent: Your AI-Powered Analytics Sidekick', 'floating-awesome-button' ),
                'message' => __( 'Stop guessing and start knowing. With DataAgent you can chat with your site data, uncover what really moves your visitors, and turn insights into action—all within WordPress. Ready to boost your conversions and grow with confidence?', 'floating-awesome-button' ),
                'buttons' => $buttons,
            )
        );

        View::RenderStatic( 'Backend.admin-notice', $data );
    }

    /**
     * Handle notice action.
     *
     * @param WP_REST_Request $request The request object.
     * @return void
     */
    public function fab_handle_notice_action( $request ) {
        if ( 'fab_install_dataagent' === $request->get_param( 'action' ) ) {
            // Dismiss notice.
            update_option( sprintf( 'fab_admin_notice_%s', $this->id ), true );
        } elseif ( 'fab_dismiss_cross_promote_dataagent' === $request->get_param( 'action' ) ) {
            // Dismiss notice.
            update_option( sprintf( 'fab_admin_notice_%s', $this->id ), true );
        }
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Show admin DataAgent promotion notice.
        add_action( 'admin_notices', array( __CLASS__, 'jit_fab_show_admin_notice' ) );

        // Handle notice action.
        add_action( 'fab_notice_action', array( __CLASS__, 'jit_fab_handle_notice_action' ) );
    }
}
