<?php

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

/**
 * Main Admin Class for Changeloger Plugin
 */
class Changeloger_Admin {

	private static $instance;
	private $dashboard;
	private $block_analytics;
	private $settings;
	private $releases;
	private $subscribers;
	private $feedback;
	private $roadmap;
	private $analytics;

	/**
	 * Get class instance
	 *
	 * @return self
	 */
	public static function get_instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->includes();
		$this->init_admin_pages();
		add_action( 'admin_menu', [ $this, 'add_admin_menu' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_styles' ], 5 );
	}

	/**
	 * Include admin class files
	 *
	 * @return void
	 */
	private function includes(): void {
		require_once __DIR__ . '/class-changeloger-dashboard.php';
		require_once __DIR__ . '/class-changeloger-block-analytics.php';
		// Settings are now handled by CSF in includes/options/settings-options.php
		require_once __DIR__ . '/class-changeloger-releases.php';
		
		// Always load promo classes in free plugin
		// Premium classes are provided by changeloger-pro plugin
		require_once __DIR__ . '/class-changeloger-subscribers-promo.php';
		require_once __DIR__ . '/class-changeloger-feedback.php';
		require_once __DIR__ . '/class-changeloger-roadmap-promo.php';
		require_once __DIR__ . '/class-changeloger-analytics-promo.php';

		// Setup Wizard for first-time onboarding.
		require_once __DIR__ . '/class-changeloger-setup-wizard.php';
	}

	/**
	 * Check if user has premium access
	 * Uses global cha_is_premium() which checks both Freemius license
	 * AND Changeloger_Pro class existence (following EazyDocs pattern).
	 *
	 * @return bool
	 */
	private function is_premium(): bool {
		return function_exists( 'cha_is_premium' ) && cha_is_premium();
	}

	/**
	 * Initialize admin page instances
	 *
	 * @return void
	 */
	private function init_admin_pages(): void {
		$this->dashboard = new Changeloger_Dashboard();
		$this->block_analytics = new Changeloger_Block_Analytics();
		// Settings are now handled by CSF
		$this->releases = new Changeloger_Releases();

		// Setup Wizard for first-time onboarding.
		Changeloger_Setup_Wizard::get_instance();

		// Feedback is now fully in free version, so we init early for enqueue hooks
		$this->feedback = Changeloger_Feedback::get_instance();

		// Subscribers, Roadmap and Analytics are initialized lazily
		// to allow changeloger-pro to load its classes first
	}

	/**
	 * Add admin menu and submenus
	 *
	 * @return void
	 */
	public function add_admin_menu(): void {
		// Add main Changeloger menu
		add_menu_page(
			__( 'Changeloger', 'changeloger' ),
			__( 'Changeloger', 'changeloger' ),
			'manage_options',
			'changeloger',
			[ $this->dashboard, 'render_page' ],
			'data:image/svg+xml;base64,' . base64_encode(
                file_get_contents(plugin_dir_path(__DIR__) . 'assets/icons/changeloger.svg')
            ),
			30
		);

		// Add Dashboard submenu (replaces the default 'Changeloger' submenu label)
		add_submenu_page(
			'changeloger',
			__( 'Dashboard', 'changeloger' ),
			__( 'Dashboard', 'changeloger' ),
			'manage_options',
			'changeloger',
			[ $this->dashboard, 'render_page' ]
		);

		// Add Changeloger Block analytics submenu
		add_submenu_page(
			'changeloger',
			__( 'Changeloger Block', 'changeloger' ),
			__( 'Changeloger Block', 'changeloger' ),
			'manage_options',
			'changeloger-block',
			[ $this->block_analytics, 'render_page' ]
		);

		// Settings submenu is created automatically by CSF in includes/options/settings-options.php

		// Add Releases submenu
		add_submenu_page(
			'changeloger',
			__( 'Releases', 'changeloger' ),
			__( 'Releases', 'changeloger' ),
			'manage_options',
			'changeloger-releases',
			[ $this->releases, 'render_page' ]
		);

		// Add Feedback submenu
		add_submenu_page(
			'changeloger',
			__( 'Feedback', 'changeloger' ),
			__( 'Feedback', 'changeloger' ),
			'manage_options',
			'changeloger-feedback',
			[ $this, 'render_feedback_page' ]
		);

		// Add Roadmap submenu
		add_submenu_page(
			'changeloger',
			__( 'Roadmap', 'changeloger' ),
			__( 'Roadmap', 'changeloger' ),
			'manage_options',
			'changeloger-roadmap',
			[ $this, 'render_roadmap_page' ]
		);
		
		// Add Subscribers submenu
		add_submenu_page(
			'changeloger',
			__( 'Subscribers', 'changeloger' ),
			__( 'Subscribers', 'changeloger' ),
			'manage_options',
			'changeloger-subscribers',
			[ $this, 'render_subscribers_page' ]
		);

		// Add Analytics submenu
		add_submenu_page(
			'changeloger',
			__( 'Analytics', 'changeloger' ),
			__( 'Analytics', 'changeloger' ),
			'manage_options',
			'changeloger-analytics',
			[ $this, 'render_analytics_page' ]
		);
	}

	/**
	 * Render subscribers page - lazy loads the correct class
	 *
	 * @return void
	 */
	public function render_subscribers_page(): void {
		$this->get_subscribers()->render_page();
	}

	/**
	 * Render feedback page - lazy loads the correct class
	 *
	 * @return void
	 */
	public function render_feedback_page(): void {
		$this->get_feedback()->render_page();
	}

	/**
	 * Render roadmap page - lazy loads the correct class
	 *
	 * @return void
	 */
	public function render_roadmap_page(): void {
		$this->get_roadmap()->render_page();
	}

	/**
	 * Render analytics page (Pro only)
	 *
	 * @return void
	 */
	public function render_analytics_page(): void {
		$this->get_analytics()->render_page();
	}

	/**
	 * Get dashboard instance
	 *
	 * @return Changeloger_Dashboard
	 */
	public function get_dashboard() {
		return $this->dashboard;
	}



	/**
	 * Get block analytics instance
	 *
	 * @return Changeloger_Block_Analytics
	 */
	public function get_block_analytics() {
		return $this->block_analytics;
	}

	/**
	 * Get settings instance
	 *
	 * @return mixed
	 */
	public function get_settings() {
		return $this->settings;
	}

	/**
	 * Get subscribers instance - lazy loads the correct class
	 * Uses premium class from changeloger-pro if available, else promo page
	 *
	 * @return object
	 */
	public function get_subscribers() {
		if ( null === $this->subscribers ) {
			if ( $this->is_premium() ) {
				$premium_subscribers = $this->get_premium_page_instance( 'Changeloger_Subscribers' );
				if ( null !== $premium_subscribers ) {
					$this->subscribers = $premium_subscribers;
				} else {
					$this->subscribers = Changeloger_Subscribers_Promo::get_instance();
				}
			} else {
				$this->subscribers = Changeloger_Subscribers_Promo::get_instance();
			}
		}
		return $this->subscribers;
	}

	/**
	 * Get feedback instance
	 *
	 * @return Changeloger_Feedback
	 */
	public function get_feedback() {
		if ( null === $this->feedback ) {
			$this->feedback = Changeloger_Feedback::get_instance();
		}
		return $this->feedback;
	}

	/**
	 * Get roadmap instance - lazy loads the correct class
	 * Uses premium class from changeloger-pro if available, else promo page
	 *
	 * @return object
	 */
	public function get_roadmap() {
		if ( null === $this->roadmap ) {
			if ( $this->is_premium() ) {
				$premium_roadmap = $this->get_premium_page_instance( 'Changeloger_Roadmap' );
				if ( null !== $premium_roadmap ) {
					$this->roadmap = $premium_roadmap;
				} else {
					$this->roadmap = Changeloger_Roadmap_Promo::get_instance();
				}
			} else {
				$this->roadmap = Changeloger_Roadmap_Promo::get_instance();
			}
		}
		return $this->roadmap;
	}

	/**
	 * Get analytics instance - lazy loads the correct class
	 * Uses premium class from changeloger-pro if available, else promo page
	 *
	 * @return object
	 */
	public function get_analytics() {
		if ( null === $this->analytics ) {
			if ( $this->is_premium() ) {
				$premium_analytics = $this->get_premium_page_instance( 'Changeloger_Analytics' );
				if ( null !== $premium_analytics ) {
					$this->analytics = $premium_analytics;
				} else {
					$this->analytics = Changeloger_Analytics_Promo::get_instance();
				}
			} else {
				$this->analytics = Changeloger_Analytics_Promo::get_instance();
			}
		}
		return $this->analytics;
	}

	/**
	 * Resolve premium page class instance across Pro singleton patterns.
	 *
	 * Supports both `get_instance()` and `instance()` for compatibility.
	 *
	 * @param string $class_name Premium class name.
	 * @return object|null
	 */
	private function get_premium_page_instance( string $class_name ) {
		if ( ! class_exists( $class_name ) ) {
			return null;
		}

		if ( is_callable( [ $class_name, 'get_instance' ] ) ) {
			return call_user_func( [ $class_name, 'get_instance' ] );
		}

		if ( is_callable( [ $class_name, 'instance' ] ) ) {
			return call_user_func( [ $class_name, 'instance' ] );
		}

		return null;
	}

	/**
	 * Enqueue global styles on all Changeloger admin pages
	 *
	 * @param string $hook Current admin page hook.
	 * @return void
	 */
	public function enqueue_global_styles( string $hook ): void {
		// Check if current page is a Changeloger page
		if ( strpos( $hook, 'changeloger' ) !== false ) {
			wp_enqueue_style(
				'cha-admin-global',
				CHANGELOGER_ASSETS . 'css/admin-global.css',
				[],
				'1.0.0'
			);

			// Shared admin utilities (confirm modal, product actions).
			wp_enqueue_script(
				'cha-admin-global-js',
				CHANGELOGER_ASSETS . 'js/admin-global.js',
				[ 'jquery' ],
				'1.0.0',
				true
			);
		}
	}
}


add_action( 'admin_init', 'cha_hide_notices_on_dashboard' );

if ( ! function_exists( 'cha_hide_notices_on_dashboard' ) ) {
	/**
	 * Hide admin notices on Changeloger pages
	 *
	 * @return void
	 */
	function cha_hide_notices_on_dashboard(): void {
		$page = $_GET['page'] ?? '';
		if ( in_array( $page, [ 'changeloger', 'changeloger-settings', 'changeloger-block', 'changeloger-releases', 'changeloger-subscribers', 'changeloger-feedback', 'changeloger-roadmap', 'changeloger-analytics', 'changeloger-account', 'changeloger-setup' ] ) ) {
			// Remove all admin notices
			remove_all_actions( 'admin_notices' );
			remove_all_actions( 'all_admin_notices' );
		}
	}
}
