<?php
/**
 * Loader class for setting up admin navigation, settings, and assets.
 *
 * @package    100Prints
 */

class Hundred_Prints_Admin_Loader {

	/**
	 * Register admin navigation menu items.
	 */
	public function register_admin_pages() {
		// Base64 encoded logo mark
		$icon_svg = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTYgMjU2Ij48cmVjdCB3aWR0aD0iMjU2IiBoZWlnaHQ9IjI1NiIgZmlsbD0ibm9uZSIvPjxwb2x5bGluZSBwb2ludHM9IjY0IDgwIDY0IDQwIDE5MiA0MCAxOTIgODAiIGZpbGw9Im5vbmUiIHN0cm9rZT0iY3VycmVudENvbG9yIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS13aWR0aD0iMTYiLz48cmVjdCB4PSIzMiIgeT0iODAiIHdpZHRoPSIxOTIiIGhlaWdodD0iMTIwIiByeD0iMTYiIGZpbGw9Im5vbmUiIHN0cm9rZT0iY3VycmVudENvbG9yIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS13aWR0aD0iMTYiLz48cG9seWxpbmUgcG9pbnRzPSI4MCAxNjAgODAgMjE2IDE3NiAyMTYgMTc2IDE2MCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSJjdXJyZW50Q29sb3IiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIxNiIvPjxjaXJjbGUgY3g9IjE5NiIgY3k9IjExNiIgcj0iMTIiIGZpbGw9ImN1cnJlbnRDb2xvciIvPjwvc3ZnPg==';

		// Top-level Menu
		add_menu_page(
			'100Prints',
			'100Prints',
			'manage_options',
			'100prints',
			[ $this, 'render_dashboard_page' ],
			$icon_svg,
			80
		);

		// Submenus
		add_submenu_page(
			'100prints',
			'Dashboard',
			'Dashboard',
			'manage_options',
			'100prints',
			[ $this, 'render_dashboard_page' ]
		);

		add_submenu_page(
			'100prints',
			'Templates',
			'Templates',
			'manage_options',
			'100prints-templates',
			[ $this, 'render_templates_page' ]
		);

		add_submenu_page(
			'100prints',
			'Generation History',
			'Generation History',
			'manage_options',
			'100prints-generation-history',
			[ $this, 'render_generation_history_page' ]
		);

		add_submenu_page(
			'100prints',
			'Analytics',
			'Analytics',
			'manage_options',
			'100prints-analytics',
			[ $this, 'render_analytics_page' ]
		);

		add_submenu_page(
			'100prints',
			'Connections',
			'Connections',
			'manage_options',
			'100prints-connections',
			[ $this, 'render_connections_page' ]
		);

		add_submenu_page(
			'100prints',
			'Settings',
			'Settings',
			'manage_options',
			'100prints-settings',
			[ $this, 'render_settings_page' ]
		);

		add_submenu_page(
			'100prints',
			'Help & Support',
			'Help & Support',
			'manage_options',
			'100prints-help',
			[ $this, 'render_help_page' ]
		);
	}

	/**
	 * Enqueue stylesheet and script files.
	 *
	 * @param string $hook The current admin page screen ID.
	 */
	public function enqueue_admin_assets( $hook ) {
		// Only enqueue on our plugin pages (case-insensitive check)
		if ( stripos( $hook, '100prints' ) === false ) {
			return;
		}

		// Google Font (Plus Jakarta Sans)
		wp_enqueue_style( '100prints-font', HUNDRED_PRINTS_URL . 'assets/admin/vendors/plus-jakarta-sans/all.css', [], HUNDRED_PRINTS_VERSION );

		// CSS icons (Phosphor)
		wp_enqueue_style( 'phosphor-icons', HUNDRED_PRINTS_URL . 'assets/admin/vendors/phosphor-icons/css/icons.css', [], '1.4.2' );

		// Custom admin styles
		$admin_css_path = HUNDRED_PRINTS_PATH . 'assets/admin/admin.css';
		$admin_css_ver  = file_exists( $admin_css_path ) ? filemtime( $admin_css_path ) : HUNDRED_PRINTS_VERSION;
		wp_enqueue_style( '100prints-admin-css', HUNDRED_PRINTS_URL . 'assets/admin/admin.css', [], $admin_css_ver );

		// Analytics page — inline CSS for chart bars and table rows
		if ( stripos( $hook, '100prints-analytics' ) !== false ) {
			$analytics_css = '
				.hundredp-chart-bar { transition: background 150ms ease, opacity 150ms ease; opacity: 0.85; }
				.hundredp-chart-bar:hover { background: #0284C7 !important; opacity: 1; }
				.hundredp-table-row { transition: background 150ms ease; }
				.hundredp-table-row:hover { background: #F8FAFC; }
			';
			wp_add_inline_style( '100prints-admin-css', $analytics_css );
		}

		// Settings page — inline CSS for the tab system
		if ( stripos( $hook, '100prints-settings' ) !== false ) {
			$settings_css = '
				.hundredp-tabs { display: flex; flex-direction: column; }
				.hundredp-tab-nav { display: flex; border-bottom: 1px solid var(--hundredp-border); background: var(--hundredp-surface); padding: 0 28px; gap: 16px; }
				.hundredp-tab { padding: 16px 8px; font-size: 13px; font-weight: 500; color: var(--hundredp-muted); text-decoration: none; border-bottom: 2px solid transparent; cursor: pointer; line-height: 1; }
				.hundredp-tab:hover { color: var(--hundredp-text); }
				.hundredp-tab.active { color: var(--hundredp-accent); border-bottom-color: var(--hundredp-accent); font-weight: 600; }
				.hundredp-tab-content { padding: 28px; }
				.hundredp-tab-content section { display: none; }
			';
			wp_add_inline_style( '100prints-admin-css', $settings_css );
		}

		$sdk_path    = HUNDRED_PRINTS_PATH . 'assets/js/wordpress-sdk.umd.js';
		$compat_path = HUNDRED_PRINTS_PATH . 'assets/js/sdk-compat.js';

		// Enqueue the SDK preview scripts on plugin admin pages.
		wp_enqueue_script( '100prints-sdk', HUNDRED_PRINTS_URL . 'assets/js/wordpress-sdk.umd.js', [], file_exists( $sdk_path ) ? filemtime( $sdk_path ) : HUNDRED_PRINTS_VERSION, true );
		wp_script_add_data( '100prints-sdk', 'defer', true );
		wp_enqueue_script( '100prints-sdk-compat', HUNDRED_PRINTS_URL . 'assets/js/sdk-compat.js', [ '100prints-sdk' ], file_exists( $compat_path ) ? filemtime( $compat_path ) : HUNDRED_PRINTS_VERSION, true );
		wp_script_add_data( '100prints-sdk-compat', 'defer', true );

		// Custom admin JS
		$admin_js_path = HUNDRED_PRINTS_PATH . 'assets/admin/admin.js';
		$admin_js_ver  = file_exists( $admin_js_path ) ? filemtime( $admin_js_path ) : HUNDRED_PRINTS_VERSION;
		wp_enqueue_script( '100prints-admin-js', HUNDRED_PRINTS_URL . 'assets/admin/admin.js', [ 'jquery', '100prints-sdk-compat' ], $admin_js_ver, true );

		// Localize for AJAX calls
		wp_localize_script( '100prints-admin-js', 'hundredPrintsAdmin', [
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			'restUrl' => esc_url_raw( rest_url( '100prints/v1/' ) ),
			'nonce'   => wp_create_nonce( 'wp_rest' ),
		] );
	}

	/**
	 * Handle redirect to dashboard page on initial activation.
	 */
	public function handle_welcome_redirect() {
		if ( get_transient( '100prints_welcome' ) && ! isset( $_GET['activate-multi'] ) ) {
			delete_transient( '100prints_welcome' );
			wp_safe_redirect( admin_url( 'admin.php?page=100prints&welcome=1' ) );
			exit;
		}
	}

	/**
	 * Register settings for settings page.
	 */
	public function register_plugin_settings() {
		register_setting( '100p_settings_group', '100p_settings', [
			'sanitize_callback' => [ $this, 'sanitize_settings' ],
			'default'           => hundred_prints_default_settings(),
		] );
	}

	/**
	 * Sanitize plugin settings array.
	 */
	public function sanitize_settings( $input ) {
		$sanitized = hundred_prints_sanitize_settings( $input );

		if ( class_exists( 'Hundred_Prints_Analytics_Service' ) ) {
			$analytics_service = new Hundred_Prints_Analytics_Service();
			$analytics_service->prune_old_logs( $sanitized['log_retention_days'] );
		}

		return $sanitized;
	}

	// ── Render templates for pages ────────────────────────────

	public function render_dashboard_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-dashboard.php';
	}

	public function render_templates_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-templates.php';
	}

	public function render_generation_history_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-generation-history.php';
	}

	public function render_analytics_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-analytics.php';
	}

	public function render_connections_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-connections.php';
	}

	public function render_settings_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-settings.php';
	}

	public function render_help_page() {
		require_once HUNDRED_PRINTS_PATH . 'admin/pages/page-help.php';
	}
}
