<?php
namespace Vidplex;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Store
 *
 * Singleton class that provides access to plugin data tables
 * such as channels and videos.
 */
class Store {
	/**
	 * Singleton instance of the Store class.
	 *
	 * @var self|null
	 */
	private static $instance = null;

	/**
	 * Channel table handler.
	 *
	 * Provides access to channel-related database operations.
	 *
	 * @var \Vidplex\Tables\Channel
	 */
	public $channels;

	/**
	 * Videos table handler.
	 *
	 * Provides access to video-related database operations.
	 *
	 * @var \Vidplex\Tables\Videos
	 */
	public $videos;

	/**
	 * Returns the singleton instance of the Store class.
	 *
	 * Ensures only one instance exists and provides a global access point.
	 *
	 * @return self
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Store class constructor.
	 *
	 * Loads required table classes and initializes the channel and
	 * videos handlers.
	 *
	 * @return void
	 */
	private function __construct() {
		require_once VIDPLEX_PLUGIN_DIR . 'includes/tables/class-channel.php';
		$this->channels = \Vidplex\Tables\Channel::get_instance();

		require VIDPLEX_PLUGIN_DIR . 'includes/tables/class-videos.php';
		$this->videos = \Vidplex\Tables\Videos::get_instance();

		add_filter(
			'cron_schedules',
			function ( $schedules ) {
				$schedules['fifteen_minutes'] = array(
					'interval' => MINUTE_IN_SECONDS * 15,
					'display'  => esc_html__( 'Every 15 minutes', 'vidplex' ),
				);

				$schedules['six_hours'] = array(
					'interval' => HOUR_IN_SECONDS * 6,
					'display'  => esc_html__( 'Every 6 hours', 'vidplex' ),
				);

				$schedules['monthly'] = array(
					'interval' => MONTH_IN_SECONDS,
					'display'  => esc_html__( 'Every month', 'vidplex' ),
				);

				return $schedules;
			}
		);

		add_action( 'vidplex_yt_sync', array( $this, 'refresh' ) );

		$intervals = array(
			'fifteen_minutes',
			'hourly',
			'six_hours',
			'daily',
			'weekly',
			'monthly',
		);

		$settings = \Vidplex\Helpers::advanced_settings();

		$interval_period = in_array( $settings['video_refresh_interval'], $intervals, true ) ? $settings['video_refresh_interval'] : 'hourly';

		if ( ! wp_next_scheduled( 'vidplex_yt_sync' ) ) {
			wp_schedule_event( time(), $interval_period, 'vidplex_yt_sync' );
		}
	}

	/**
	 * Refreshes channel and video data.
	 *
	 * Retrieves the latest channel details and associated video information,
	 * then updates the stored records to reflect any changes such as new uploads,
	 * metadata updates, or removed videos.
	 *
	 * This method is typically triggered by a scheduled cron event and ensures
	 * that channel and video data remain up to date based on the current settings.
	 *
	 * @return void
	 */
	public function refresh() {
		$channels = $this->channels->get_all();

		if ( ! empty( $channels ) ) {
			foreach ( $channels as $channel ) {
				$this->channels->fetch_channel_data( $channel['channel_id'] );
				$this->videos->sync_channel_videos( $channel['channel_id'] );
			}

			return true;
		}
	}
}
