<?php
/**
 * Upgrade to Pro Admin Notice
 *
 * Displays a dismissable admin notice encouraging users to upgrade to the Pro version
 * with three action buttons: Upgrade to Pro, Get Started, and Live Preview.
 *
 * @package     Business Interface
 * @author      WPInterface Team
 * @since       1.0.0
 */

/**
 * Do not allow direct script access.
 */
if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('Business_Interface_Upgrade_Notice')) :

    /**
     * Upgrade Notice Class
     */
    class Business_Interface_Upgrade_Notice
    {

        /**
         * Notice ID
         *
         * @var string
         */
        private $notice_id = 'business_interface_notice_upgrade-to-pro';

        /**
         * Primary class constructor.
         *
         * @since 1.0.0
         */
        public function __construct()
        {
            add_action('admin_notices', array($this, 'display_upgrade_notice'));
        }

        /**
         * Display upgrade to pro notice
         *
         * @return void
         * @since 1.0.0
         */
        public function display_upgrade_notice()
        {

            // Only show to users who can manage options
            if (!current_user_can('manage_options')) {
                return;
            }

            $screen = get_current_screen();

            // Display on Dashboard and Themes pages only
            if (!in_array($screen->base, array('dashboard', 'themes'), true)) {
                return;
            }

            // Don't display if already dismissed
            if (business_interface_is_notice_dismissed('upgrade-to-pro')) {
                return;
            }

            // Build the notice message
            $message = $this->get_notice_message();

            // Display the notice
            $this->render_notice($message);
        }

        /**
         * Get notice message content
         *
         * @return string
         * @since 1.0.0
         */
        private function get_notice_message()
        {
            $message = '<strong>' . esc_html__('Unlock the Full Potential of Business Interface!', 'business-interface') . '</strong><br>';
            $message .= esc_html__('Upgrade to Pro and get access to premium features, advanced customization options, priority support, and much more.', 'business-interface');
            return $message;
        }

        /**
         * Render the admin notice
         *
         * @param string $message Notice message content.
         * @return void
         * @since 1.0.0
         */
        private function render_notice($message)
        {
            ?>
            <div id="business-interface-upgrade-notice"
                 class="notice notice-info is-dismissible business-interface-notice business-interface-upgrade-notice">
                <div class="business-interface-upgrade-notice-content">

                    <div class="business-interface-upgrade-notice-message">
                        <p><?php echo wp_kses_post($message); ?></p>
                        <p class="business-interface-upgrade-notice-actions">
                            <a href="<?php echo esc_url('https://wpinterface.com/themes/business-interface/#choose-pricing-plan'); ?>"
                               class="button button-primary interface-button primary" target="_blank"
                               rel="noopener noreferrer">
                                <span class="dashicons dashicons-unlock"></span>
                                <?php esc_html_e('Upgrade to Pro', 'business-interface'); ?>
                            </a>
                            <a href="<?php echo esc_url(admin_url('admin.php?page=business-interface-dashboard')); ?>"
                               class="button button-secondary interface-button secondary">
                                <span class="dashicons dashicons-admin-settings"></span>
                                <?php esc_html_e('Get Started', 'business-interface'); ?>
                            </a>
                            <a href="<?php echo esc_url('https://wpinterface.com/themes/business-interface/#demo-links'); ?>"
                               class="button button-secondary interface-button secondary" target="_blank"
                               rel="noopener noreferrer">
                                <span class="dashicons dashicons-welcome-view-site"></span>
                                <?php esc_html_e('Live Preview', 'business-interface'); ?>
                            </a>
                        </p>
                    </div>
                </div>
            </div>

            <style type="text/css">
                .business-interface-upgrade-notice-actions .button {
                    display: inline-flex;
                    align-items: center;
                    gap: 6px;
                }

                .business-interface-upgrade-notice-actions .button .dashicons {
                    font-size: 16px;
                }

                .business-interface-upgrade-notice-actions .button-primary .dashicons.dashicons-unlock {
                    color: #fff;
                }

                @media screen and (max-width: 782px) {
                    .business-interface-upgrade-notice-actions .button {
                        justify-content: center;
                        width: 100%;
                    }
                }
            </style>

            <script type="text/javascript">
                jQuery(document).ready(function ($) {
                    var $notice = $('#business-interface-upgrade-notice');

                    // Handle dismiss button click
                    $notice.on('click', '.notice-dismiss', function (event) {
                        var nonce = "<?php echo esc_attr(wp_create_nonce('business_interface_dismiss_notice')); ?>";

                        $.post(ajaxurl, {
                            action: 'business_interface_dismiss_notice',
                            msgid: 'business_interface_notice_upgrade-to-pro',
                            expires: <?php echo esc_js(30 * 24 * 60 * 60); ?>, // 30 days
                            _ajax_nonce: nonce,
                        });

                        $notice.fadeTo(100, 0, function () {
                            $notice.slideUp(100, function () {
                                $notice.remove();
                            });
                        });
                    });
                });
            </script>
            <?php
        }
    }

endif;

// Initialize the upgrade notice
new Business_Interface_Upgrade_Notice();
