<?php
/**
 * Core plugin functionality
 *
 * @package BubblaV_Chat
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Core plugin class
 */
class Bubblav_Core {

	/**
	 * Admin instance
	 *
	 * @var Bubblav_Admin|null
	 */
	private $admin = null;

	/**
	 * Widget instance
	 *
	 * @var Bubblav_Widget|null
	 */
	private $widget = null;

	/**
	 * WooCommerce instance
	 *
	 * @var Bubblav_WooCommerce|null
	 */
	private $woocommerce = null;

	/**
	 * REST API instance
	 *
	 * @var Bubblav_REST_API|null
	 */
	private $rest_api = null;

	/**
	 * Initialize the plugin
	 *
	 * @return void
	 */
	public function init() {
		// Initialize components.
		$this->init_components();

		// Register hooks.
		$this->register_hooks();
	}

	/**
	 * Initialize plugin components
	 *
	 * @return void
	 */
	private function init_components() {
		// Always initialize admin, widget, and REST API.
		$this->admin    = new Bubblav_Admin();
		$this->widget   = new Bubblav_Widget();
		$this->rest_api = new Bubblav_REST_API();

		// Initialize REST API routes.
		$this->rest_api->init();

		// Check for WooCommerce.
		if ( $this->is_woocommerce_active() ) {
			$this->woocommerce = new Bubblav_WooCommerce();
		}
	}

	/**
	 * Register WordPress hooks
	 *
	 * @return void
	 */
	private function register_hooks() {
		// Admin hooks.
		add_action( 'admin_menu', array( $this->admin, 'add_admin_menu' ) );
		add_action( 'admin_init', array( $this->admin, 'register_settings' ) );
		add_action( 'admin_post_bubblav_save_settings', array( $this->admin, 'save_settings' ) );
		add_action( 'admin_post_bubblav_disconnect', array( $this->admin, 'disconnect' ) );
		add_action( 'admin_post_bubblav_start_oauth', array( $this->admin, 'start_oauth' ) );
		add_action( 'admin_post_bubblav_oauth_callback', array( $this->admin, 'oauth_callback' ) );
		add_action( 'admin_notices', array( $this->admin, 'show_admin_notices' ) );
		add_action( 'admin_enqueue_scripts', array( $this->admin, 'enqueue_admin_assets' ) );

		// Frontend hooks.
		add_action( 'wp_footer', array( $this->widget, 'inject_widget' ), 999 );

		// WooCommerce hooks.
		if ( $this->woocommerce ) {
			add_action( 'woocommerce_init', array( $this->woocommerce, 'init_integration' ) );
		}

		// Scheduled cleanup.
		if ( ! wp_next_scheduled( 'bubblav_cleanup_expired_oauth' ) ) {
			wp_schedule_event( time(), 'daily', 'bubblav_cleanup_expired_oauth' );
		}
		add_action( 'bubblav_cleanup_expired_oauth', array( $this, 'cleanup_expired_oauth' ) );
	}

	/**
	 * Check if WooCommerce is active
	 *
	 * @return bool True if WooCommerce is active
	 */
	public function is_woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}

	/**
	 * Get connection status
	 *
	 * @return string Connection status
	 */
	public function get_connection_status() {
		return get_option( 'bubblav_connection_status', 'disconnected' );
	}

	/**
	 * Get website ID
	 *
	 * @return string Website ID or empty string
	 */
	public function get_website_id() {
		return get_option( 'bubblav_website_id', '' );
	}

	/**
	 * Check if plugin is connected
	 *
	 * @return bool True if connected
	 */
	public function is_connected() {
		return $this->get_connection_status() === 'connected' && ! empty( $this->get_website_id() );
	}

	/**
	 * Cleanup expired OAuth transients
	 *
	 * @return void
	 */
	public function cleanup_expired_oauth() {
		// This is a no-op in the plugin itself.
		// The server-side cleanup handles expired OAuth states.
	}
}
