<?php

/**
 * Admin Hooks
 * Registers all admin action hooks and filters
 *
 * @since 1.1.0
 * @package EasyVideoReviews
 */

// Namespace.
namespace EasyVideoReviews\Admin;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit( 1 );

if ( ! class_exists( __NAMESPACE__ . '\Hooks' ) ) {

    /**
     * Admin Hooks
     *
     * @since 1.1.0
     */
    class Hooks extends \EasyVideoReviews\Base\Controller {

        /**
         * Registers all admin hooks
         *
         * @return void
         */
        public function register_hooks() {
            // admin init.
            // $this->init_appsero();
            add_action( 'init', array( $this, 'init_appsero' ) );
            add_filter( 'wppool_plugins', [ $this, 'change_campaign_button_link' ], 10, 1 );
        }

        /**
         * Init Appsero
         *
         * @return void
         */
        public function init_appsero() {

            add_filter( 'appsero_is_local', '__return_false' );

            if ( ! class_exists( 'EVR\Appsero\Client' ) ) {
                require_once EASY_VIDEO_REVIEWS_INCLUDES . 'appsero/Client.php';
            }

            $client = new \EVR\Appsero\Client( '98bea24e-5324-4ccf-932a-6f1c77b67ad6', 'Easy Video Reviews', EASY_VIDEO_REVIEWS_FILE );
            $client->set_textdomain( 'easy-video-reviews' );
            $client->insights()->init();

            if ( function_exists( 'wppool_plugin_init' ) ) {
                $evr_plugin = wppool_plugin_init( 'easy_video_reviews', EASY_VIDEO_REVIEWS_URL . 'includes/wppool/background-image.png' );

                // Campain.
                $campain_image = EASY_VIDEO_REVIEWS_URL . 'includes/wppool/black-friday.png';

                if ( $evr_plugin && is_object( $evr_plugin ) && method_exists( $evr_plugin, 'set_campaign' ) ) {
                    $to = '2025-12-04';
                    $from = '2025-11-17';
                    $cta_text = 'Grab Your Deals';

                    $evr_plugin->set_campaign( $campain_image, $to, $from, $cta_text );
                }
            }
        }

        /**
         * Changes the 'button_link' for the 'easy_video_reviews'
         * plugin data during a specified campaign period.
         * * This method is intended to be hooked into the 'wppool_plugins' filter.
         * The link is reverted to its original value after the campaign end date (December 4th).
         *
         * @param array $plugins An array containing data for all registered WP Pool plugins.
         * @return array The potentially modified array of plugin data.
         */
        public function change_campaign_button_link( $plugins ) {
            $start_date = strtotime( '2025-11-17' );
            $end_date   = strtotime( '2025-12-04' );
            $current_time = current_time( 'timestamp' );
            if ( $current_time >= $start_date && $current_time <= $end_date ) {
                $plugin_key = 'easy_video_reviews';
                $new_campaign_url = 'https://lnk.wppool.dev/YuGP8q3';
                if ( isset( $plugins[ $plugin_key ] ) ) {
                    $plugins[ $plugin_key ]['button_link'] = $new_campaign_url;

                }
            }
            return $plugins;
        }
    }

    // Initialize the class.
    Hooks::init();
}
