<?php

namespace fluXtore\Core\Admin;

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

if ( ! class_exists( 'fluXtore_Update_Notice' ) ) {
    class fluXtore_Update_Notice {
        /**
         * @var self|null
         */
        private static $instance = null;

        /**
         * @var bool
         */
        private $show_notice = false;

        private function __construct() {}

        public static function init() {
            if ( null !== self::$instance ) {
                return;
            }

            self::$instance = new self();

            add_action( 'admin_init', [ self::$instance, 'check_version' ] );
            add_action( 'admin_notices', [ self::$instance, 'render_notice' ] );
        }

        public function check_version() {
            $stored_version = get_option( 'fluxtore_version' );

            if ( defined( 'FLUXTORE_VERSION' ) && FLUXTORE_VERSION !== $stored_version ) {
                $this->show_notice = true;
                update_option( 'fluxtore_version', FLUXTORE_VERSION );
            }
        }

        public function render_notice() {
            if ( ! $this->show_notice ) {
                return;
            }
            ?>
            <div class="notice notice-warning is-dismissible">
                <p><?php esc_html_e( 'fluXtore has been updated. Please clear your browser cache to ensure the changes take effect.', 'fluxtore' ); ?></p>
            </div>
            <?php
        }

        public static function activate() {
            if ( defined( 'FLUXTORE_VERSION' ) && ! get_option( 'fluxtore_version' ) ) {
                update_option( 'fluxtore_version', FLUXTORE_VERSION );
            }
        }
    }
}

fluXtore_Update_Notice::init();
