<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\View;

/**
 * No Published FAB Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class No_Published_Fab_Notice extends Notice implements JIT_Interface {

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

    /**
     * Show admin notice when no FAB is published.
     *
     * @return void
     */
    public function fab_show_admin_notice() {
        // Check if the notice should be shown.
        if ( ! $this->should_show_notice() ) {
            return;
        }

        // Check if any FAB is published.
        $args = array(
            'post_type'      => 'fab',
            'post_status'    => 'publish',
            'posts_per_page' => 1,
        );

        $query = new \WP_Query( $args );

        // If there's at least one published FAB, don't show the notice.
        if ( $query->have_posts() ) {
            return;
        }

        // Setup Buttons.
        $buttons   = array();
        $buttons[] = array(
            'message' => __( 'Create Your First FAB →', 'floating-awesome-button' ),
            'url'     => admin_url( 'edit.php?post_type=fab&page=floating-awesome-button-templates' ),
            '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',
        );

        // Prepare the data for the admin notice.
        $data = array_merge(
            $this->get_default_data(),
            array(
                'title'   => __( '🚀 Get Started with Floating Awesome Button!', 'floating-awesome-button' ),
                'message' => __( 'You haven\'t published any Floating Awesome Buttons yet. Create your first FAB to enhance your website with interactive buttons!', 'floating-awesome-button' ),
                'buttons' => $buttons,
            )
        );

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

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

    /**
     * Execute Class.
     *
     * @return void
     */
    public static function jit() {
        // Add admin notice.
        add_action( 'admin_notices', array( __CLASS__, 'jit_fab_show_admin_notice' ) );
    }
}
