<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\Branding;

/**
 * Manages dark/light theme preference per user.
 *
 * Stores preference in user_meta and provides AJAX endpoint for toggling.
 * Migrated from stryke-branding (StrykeWP\Branding\ThemePreference).
 *
 * @package StrykeWP\Modules\Branding
 */
class ThemePreference {

	private const META_KEY     = 'stryke_theme_mode';
	private const NONCE_ACTION = 'stryke_admin_nonce';

	// -----------------------------------------------------------------------
	// Hook Registration
	// -----------------------------------------------------------------------

	public function register_hooks(): void {
		add_action( 'admin_init', array( $this, 'register_color_scheme' ) );
		add_action( 'admin_init', array( $this, 'disable_other_schemes' ), 11 );
		add_filter( 'get_user_option_admin_color', array( $this, 'force_color_scheme' ) );
		add_action( 'user_register', array( $this, 'set_default_for_new_user' ) );
		add_action( 'wp_ajax_stryke_set_theme', array( $this, 'ajax_save' ) );
	}

	// -----------------------------------------------------------------------
	// Public API
	// -----------------------------------------------------------------------

	/**
	 * Get the current user's theme mode.
	 */
	public function get_current_mode(): string {
		$user_id = get_current_user_id();
		if ( ! $user_id ) {
			return 'dark';
		}

		$mode = get_user_meta( $user_id, self::META_KEY, true );
		return in_array( $mode, array( 'dark', 'light' ), true ) ? $mode : 'dark';
	}

	/**
	 * Get the body class for the current theme mode.
	 */
	public function get_body_class(): string {
		return 'stryke-' . $this->get_current_mode();
	}

	// -----------------------------------------------------------------------
	// AJAX
	// -----------------------------------------------------------------------

	/**
	 * AJAX handler to save theme preference.
	 */
	public function ajax_save(): void {
		check_ajax_referer( self::NONCE_ACTION, '_wpnonce' );

		if ( ! current_user_can( 'read' ) ) {
			wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'stryke-wp' ) ), 403 );
		}

		$mode = sanitize_text_field( wp_unslash( $_POST['mode'] ?? 'dark' ) );
		if ( ! in_array( $mode, array( 'dark', 'light' ), true ) ) {
			$mode = 'dark';
		}

		update_user_meta( get_current_user_id(), self::META_KEY, $mode );
		wp_send_json_success( array( 'mode' => $mode ) );
	}

	// -----------------------------------------------------------------------
	// Color Scheme
	// -----------------------------------------------------------------------

	/**
	 * Register the STRYKE color scheme.
	 */
	public function register_color_scheme(): void {
		wp_admin_css_color(
			'stryke-dark',
			'STRYKE Dark',
			'',
			array( '#0d0d0d', '#141414', '#189ba7', '#147d87' )
		);
	}

	/**
	 * Force admin_color to stryke-dark so WP core never falls back to a
	 * removed scheme (e.g. 'fresh'), which would cause a null access in
	 * script-loader.php.
	 */
	public function force_color_scheme( string $color ): string {
		return 'stryke-dark';
	}

	/**
	 * Remove all other admin color schemes.
	 */
	public function disable_other_schemes(): void {
		global $_wp_admin_css_colors;

		$keep = $_wp_admin_css_colors['stryke-dark'] ?? null;
		$_wp_admin_css_colors = array();
		if ( $keep ) {
			$_wp_admin_css_colors['stryke-dark'] = $keep;
		}
	}

	/**
	 * Set dark theme as default for new users.
	 */
	public function set_default_for_new_user( int $user_id ): void {
		update_user_meta( $user_id, 'admin_color', 'stryke-dark' );
		update_user_meta( $user_id, self::META_KEY, 'dark' );
	}
}
