<?php
/**
 * Main plugin class.
 *
 * @package BOOSAIAS_For_WooCommerce
 */

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

class BOOSAIAS_Plugin {

	private static $instance = null;

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

	private function __construct() {
		if ( ! $this->is_woocommerce_active() ) {
			add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
			return;
		}

		$this->load_dependencies();
		$this->init_hooks();
	}

	private function is_woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}

	private function load_dependencies() {
		require_once BOOSAIAS_PLUGIN_DIR . 'includes/class-api-client.php';
		require_once BOOSAIAS_PLUGIN_DIR . 'includes/class-settings.php';
		require_once BOOSAIAS_PLUGIN_DIR . 'includes/class-catalog-sync.php';
		require_once BOOSAIAS_PLUGIN_DIR . 'includes/class-widget-loader.php';
		require_once BOOSAIAS_PLUGIN_DIR . 'includes/class-rest-controller.php';
	}

	private function init_hooks() {
		( new BOOSAIAS_Settings() )->init();
		( new BOOSAIAS_Catalog_Sync() )->init();
		( new BOOSAIAS_Widget_Loader() )->init();
		( new BOOSAIAS_Rest_Controller() )->init();
	}

	public function woocommerce_missing_notice() {
		echo '<div class="notice notice-error"><p>' .
			esc_html__( 'Boostanova AI Shopping Assistant requires WooCommerce to be installed and active.', 'boostanova-ai-shop-assistant' ) .
			'</p></div>';
	}

	public static function activate() {
		if ( ! get_option( 'boosaias_version' ) ) {
			add_option( 'boosaias_version', BOOSAIAS_VERSION );
		}
		// Set default free license key if not already set
		if ( ! get_option( 'boosaias_license_key' ) ) {
			add_option( 'boosaias_license_key', 'WAI-TEST-0000-0000' );
		}
		if ( ! wp_next_scheduled( 'boosaias_full_sync' ) ) {
			wp_schedule_event( time(), 'daily', 'boosaias_full_sync' );
		}
	}

	public static function deactivate() {
		wp_clear_scheduled_hook( 'boosaias_full_sync' );
	}
}
