<?php
/**
 * Microsoft Teams Integration Initialization
 *
 * @package SOVLIX\MHUB\MsTeams
 */

namespace SOVLIX\MHUB\MsTeams;

use SOVLIX\MHUB\API\MsTeams_Api;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Mhub_MsTeams_Init' ) ) {
	/**
	 * Class Mhub_MsTeams_Init
	 *
	 * Initializes Microsoft Teams integration components.
	 */
	class Mhub_MsTeams_Init {
		/**
		 * Hold my instance.
		 *
		 * @var Mhub_MsTeams_Init|null
		 */
		protected static $instance;

		/**
		 * Create only one instance so that it may not Repeat
		 */
		public static function instance() {
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Mhub_MsTeams_Init constructor.
		 */
		public function __construct() {
			// Initialize shortcodes immediately to ensure registration
			if ( class_exists( 'SOVLIX\MHUB\MsTeams\Mhub_MsTeams_Shortcodes' ) ) {
				Mhub_MsTeams_Shortcodes::instance();
			}

			add_action( 'init', array( $this, 'init_components' ) );
			add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
		}

		/**
		 * Initialize Microsoft Teams components.
		 */
		public function init_components() {
			// Initialize API classes
			if ( class_exists( 'SOVLIX\MHUB\API\MsTeams_Api' ) ) {
				\SOVLIX\MHUB\API\MsTeams_Api::get_instance();
			}

			if ( class_exists( 'SOVLIX\MHUB\MsTeams\Mhub_MsTeams_Api' ) ) {
				Mhub_MsTeams_Api::instance();
			}

			// Handle OAuth callback
			$this->handle_oauth_callback();
		}

		/**
		 * Register REST API routes.
		 */
		public function register_rest_routes() {
			// Handled by Mhub_API class
		}

		/**
		 * Handle OAuth callback from Microsoft.
		 */
		private function handle_oauth_callback() {
			if ( isset( $_GET['code'] ) && isset( $_GET['state'] ) && is_admin() ) {
				$state = sanitize_text_field( wp_unslash( $_GET['state'] ) );
				$expected_state = admin_url( 'admin.php' );
				
				if ( $state === $expected_state ) {
					$api = MsTeams_Api::get_instance();
					$api->fetch_access_token();
				}
			}
		}

		/**
		 * Enqueue frontend scripts and styles.
		 */
		public function enqueue_frontend_scripts() {
			// Microsoft Teams uses shared frontend styles
		}

		/**
		 * Enqueue admin scripts and styles.
		 */
		public function enqueue_admin_scripts() {
			// Microsoft Teams uses shared admin styles
		}

		/**
		 * Get Microsoft Teams settings.
		 *
		 * @return array
		 */
		public static function get_settings() {
			return get_option( 'mhub_msteams_settings', array() );
		}

		/**
		 * Check if Microsoft Teams is configured.
		 *
		 * @return bool
		 */
		public static function is_configured() {
			$settings = self::get_settings();
			return ! empty( $settings['client_id'] ) && ! empty( $settings['client_secret'] );
		}

		/**
		 * Check if user is connected to Microsoft Teams.
		 *
		 * @return bool
		 */
		public static function is_connected() {
			$user_id = get_current_user_id();
			$token_data = get_user_meta( $user_id, 'mhub_msteams_access_token', true );
			return ! empty( $token_data['access_token'] );
		}
	}
}