<?php
/**
 * Dark Mode — FREE
 *
 * The dark mode toggle is accessible from the Panelra panel only
 * (Appearance tab) — never in the admin bar.
 *
 * Logic:
 * - Per-user preference stored as user meta
 * - Global override (force_on / force_off) stored as option
 * - CSS injected via admin_head only when dark mode is active
 *
 * @package Panelra
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }

class Pnlrdb_Dark_Mode {

	const USER_META_KEY = 'pnlrdb_dark_mode';
	const GLOBAL_OPTION = 'pnlrdb_dark_mode_global';

	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'inject_dark_mode_css' ), 5 );
		add_filter( 'admin_body_class',  array( $this, 'add_body_class' ) );
		add_action( 'wp_ajax_pnlrdb_toggle_dark_mode', array( $this, 'ajax_toggle' ) );
	}

	/* -----------------------------------------------------------------------
	   State
	----------------------------------------------------------------------- */

	public function is_dark_mode_active() {
		$global = get_option( self::GLOBAL_OPTION, 'off' );
		if ( 'force_on'  === $global ) { return true; }
		if ( 'force_off' === $global ) { return false; }
		if ( ! is_user_logged_in() ) { return false; }
		return 'on' === get_user_meta( get_current_user_id(), self::USER_META_KEY, true );
	}

	/* -----------------------------------------------------------------------
	   Body class
	----------------------------------------------------------------------- */

	public function add_body_class( $classes ) {
		if ( $this->is_dark_mode_active() ) {
			$classes .= ' pnlrdb-dark';
		}
		return $classes;
	}

	/* -----------------------------------------------------------------------
	   CSS injection
	----------------------------------------------------------------------- */

	public function inject_dark_mode_css() {
		if ( ! $this->is_dark_mode_active() ) {
			return;
		}

		ob_start();
		?>
		body.pnlrdb-dark,
		body.pnlrdb-dark #wpcontent,
		body.pnlrdb-dark #wpbody,
		body.pnlrdb-dark #wpbody-content,
		body.pnlrdb-dark #wpfooter {
			background-color: #0f1117 !important;
			color: #e2e8f0 !important;
		}
		body.pnlrdb-dark .wrap { color: #e2e8f0 !important; }
		body.pnlrdb-dark h1,body.pnlrdb-dark h2,body.pnlrdb-dark h3,
		body.pnlrdb-dark h4,.wrap h1,.wrap h2 { color: #f1f5f9 !important; }
		body.pnlrdb-dark #wpcontent a:not(.button):not(.button-primary):not(.button-secondary):not(.page-title-action):not(.components-button):not(.media-button):not(.thickbox),
		body.pnlrdb-dark .row-actions a { color: #818cf8 !important; }
		body.pnlrdb-dark #wpcontent a:not(.button):not(.button-primary):not(.button-secondary):not(.page-title-action):not(.components-button):not(.media-button):not(.thickbox):hover,
		body.pnlrdb-dark .row-actions a:hover { color: #a5b4fc !important; }

		/* Admin bar */
		body.pnlrdb-dark #wpadminbar { background: #1e2130 !important; border-bottom: 1px solid #2d3148 !important; }
		body.pnlrdb-dark #wpadminbar .ab-item,
		body.pnlrdb-dark #wpadminbar a.ab-item,
		body.pnlrdb-dark #wpadminbar .ab-label { color: #cbd5e1 !important; }
		body.pnlrdb-dark #wpadminbar .menupop:hover > .ab-item,
		body.pnlrdb-dark #wpadminbar a.ab-item:hover { background: #2d3148 !important; color: #f1f5f9 !important; }
		body.pnlrdb-dark #wpadminbar .menupop .ab-sub-wrapper { background: #1e2130 !important; border: 1px solid #2d3148 !important; }
		body.pnlrdb-dark #wpadminbar .menupop .ab-sub-wrapper .ab-item { color: #cbd5e1 !important; }
		body.pnlrdb-dark #wpadminbar .menupop .ab-sub-wrapper .ab-item:hover { background: #2d3148 !important; color: #f1f5f9 !important; }

		/* Postbox / Metabox */
		body.pnlrdb-dark .postbox,
		body.pnlrdb-dark .postbox-container .postbox { background: #1a1f2e !important; border-color: #2d3148 !important; color: #e2e8f0 !important; }
		body.pnlrdb-dark .postbox .postbox-header,
		body.pnlrdb-dark .postbox h2.hndle { background: #1e2130 !important; border-bottom-color: #2d3148 !important; color: #f1f5f9 !important; }
		body.pnlrdb-dark .postbox .inside { color: #e2e8f0 !important; }

		/* Tables */
		body.pnlrdb-dark table.widefat,
		body.pnlrdb-dark table.wp-list-table { background: #1a1f2e !important; border-color: #2d3148 !important; color: #e2e8f0 !important; }
		body.pnlrdb-dark table.widefat thead,
		body.pnlrdb-dark table.wp-list-table thead { background: #1e2130 !important; }
		body.pnlrdb-dark table.widefat th,
		body.pnlrdb-dark table.wp-list-table th { color: #94a3b8 !important; border-bottom-color: #2d3148 !important; }
		body.pnlrdb-dark table.widefat td,
		body.pnlrdb-dark table.wp-list-table td { color: #e2e8f0 !important; border-bottom-color: #1e2535 !important; }
		body.pnlrdb-dark table.widefat tr:nth-child(odd),
		body.pnlrdb-dark table.wp-list-table tr.alternate { background: #161b28 !important; }
		body.pnlrdb-dark table.widefat tr:hover td,
		body.pnlrdb-dark table.wp-list-table tr:hover td { background: #1e2535 !important; }
		body.pnlrdb-dark .row-actions { color: #64748b !important; }
		body.pnlrdb-dark .row-actions a { color: #818cf8 !important; }

		/* Form fields */
		body.pnlrdb-dark input[type="text"],
		body.pnlrdb-dark input[type="email"],
		body.pnlrdb-dark input[type="url"],
		body.pnlrdb-dark input[type="password"],
		body.pnlrdb-dark input[type="number"],
		body.pnlrdb-dark input[type="search"],
		body.pnlrdb-dark textarea,
		body.pnlrdb-dark select { background: #1e2535 !important; border-color: #3d4566 !important; color: #e2e8f0 !important; }
		body.pnlrdb-dark input:focus,
		body.pnlrdb-dark textarea:focus,
		body.pnlrdb-dark select:focus { border-color: #6366f1 !important; box-shadow: 0 0 0 2px rgba(99,102,241,.2) !important; }
		body.pnlrdb-dark input::placeholder,
		body.pnlrdb-dark textarea::placeholder { color: #4a5568 !important; }

		/* Buttons */
		body.pnlrdb-dark .button,
		body.pnlrdb-dark .button-secondary { background: #1e2535 !important; border-color: #3d4566 !important; color: #cbd5e1 !important; }
		body.pnlrdb-dark .button:hover,
		body.pnlrdb-dark .button-secondary:hover { background: #2d3555 !important; border-color: #6366f1 !important; color: #f1f5f9 !important; }
		body.pnlrdb-dark .button-primary { background: #6366f1 !important; border-color: #4f46e5 !important; color: #fff !important; }
		body.pnlrdb-dark .button-primary:hover { background: #4f46e5 !important; }

		/* Notice */
		body.pnlrdb-dark .notice,
		body.pnlrdb-dark .updated,
		body.pnlrdb-dark div.error { background: #1e2535 !important; border-left-color: #6366f1 !important; color: #e2e8f0 !important; }
		body.pnlrdb-dark .notice-success { border-left-color: #22c55e !important; }
		body.pnlrdb-dark .notice-error   { border-left-color: #ef4444 !important; }
		body.pnlrdb-dark .notice-warning { border-left-color: #f59e0b !important; }
		body.pnlrdb-dark .notice p,
		body.pnlrdb-dark .notice-dismiss::before { color: #cbd5e1 !important; }

		/* Form table */
		body.pnlrdb-dark .form-table th { color: #94a3b8 !important; }
		body.pnlrdb-dark .form-table td { color: #e2e8f0 !important; }
		body.pnlrdb-dark .description { color: #64748b !important; }

		/* Dashboard widgets */
		body.pnlrdb-dark #dashboard-widgets .postbox { background: #1a1f2e !important; border-color: #2d3148 !important; }
		body.pnlrdb-dark .welcome-panel { background: #1e2535 !important; border-color: #2d3148 !important; }
		body.pnlrdb-dark .welcome-panel h2,
		body.pnlrdb-dark .welcome-panel p { color: #e2e8f0 !important; }

		/* Block Editor */
		body.pnlrdb-dark .interface-interface-skeleton,
		body.pnlrdb-dark .interface-interface-skeleton__body { background: #0f1117 !important; }
		body.pnlrdb-dark .interface-interface-skeleton__header { background: #1a1f2e !important; border-bottom-color: #2d3148 !important; }
		body.pnlrdb-dark .interface-interface-skeleton__sidebar { background: #1a1f2e !important; border-left-color: #2d3148 !important; }
		body.pnlrdb-dark .editor-styles-wrapper { background: #161b28 !important; color: #e2e8f0 !important; }

		/* Footer, misc */
		body.pnlrdb-dark #wpfooter { border-top-color: #2d3148 !important; color: #64748b !important; }
		body.pnlrdb-dark #wpfooter a { color: #818cf8 !important; }
		body.pnlrdb-dark hr { border-color: #2d3148 !important; }

		/* Scrollbar */
		body.pnlrdb-dark ::-webkit-scrollbar { width: 6px; height: 6px; }
		body.pnlrdb-dark ::-webkit-scrollbar-track { background: #0f1117; }
		body.pnlrdb-dark ::-webkit-scrollbar-thumb { background: #2d3555; border-radius: 3px; }
		body.pnlrdb-dark ::-webkit-scrollbar-thumb:hover { background: #4a5580; }

		/* Panelra panel */
		body.pnlrdb-dark .ap773-card { background: #1a1f2e !important; border-color: #2d3148 !important; }
		body.pnlrdb-dark .ap773-card-title { color: #f1f5f9 !important; }
		body.pnlrdb-dark .ap773-section-title { color: #f1f5f9 !important; }
		body.pnlrdb-dark .ap773-section-desc { color: #94a3b8 !important; }
		body.pnlrdb-dark .ap773-label { color: #cbd5e1 !important; }
		body.pnlrdb-dark .ap773-desc { color: #64748b !important; }
		body.pnlrdb-dark .ap773-main { background: #0f1117 !important; }
		body.pnlrdb-dark .ap773-role-card { background: #1a1f2e !important; border-color: #2d3148 !important; }
		body.pnlrdb-dark .ap773-table thead th { background: #1e2130 !important; color: #64748b !important; }
		body.pnlrdb-dark .ap773-table tbody td { color: #e2e8f0 !important; border-bottom-color: #1e2535 !important; }
		body.pnlrdb-dark .ap773-table tbody tr:hover td { background: #1e2535 !important; }
		body.pnlrdb-dark .ap773-input,
		body.pnlrdb-dark .ap773-select { background: #1e2535 !important; border-color: #3d4566 !important; color: #e2e8f0 !important; }
		body.pnlrdb-dark .ap773-toggle-slider { background: #2d3555 !important; }
		body.pnlrdb-dark .ap773-checkbox-item { color: #cbd5e1 !important; }
		body.pnlrdb-dark .ap773-form-footer { background: #0f1117 !important; border-top-color: #2d3148 !important; }

		/* ---- ADMIN PILOT PANEL ISOLATION IN DARK MODE ----
		   The Panelra sidebar always keeps its fixed colors.
		   The settings panel (.ap773-page) is not darkened — it has its own styles.
		   Only the standard WP content (#wpcontent) is darkened. */
		body.pnlrdb-dark .ap773-page .ap773-card { background: #1a1f2e !important; border-color: #2d3148 !important; }
		body.pnlrdb-dark .ap773-page .ap773-section-title { color: #f1f5f9 !important; }
		body.pnlrdb-dark .ap773-page .ap773-section-desc  { color: #94a3b8 !important; }
		body.pnlrdb-dark .ap773-page .ap773-card-title    { color: #f1f5f9 !important; }
		body.pnlrdb-dark .ap773-page .ap773-label         { color: #cbd5e1 !important; }
		body.pnlrdb-dark .ap773-page .ap773-desc          { color: #64748b !important; }
		body.pnlrdb-dark .ap773-page .ap773-main          { background: #0f1117 !important; }
		body.pnlrdb-dark .ap773-page                      { background: #0f1117 !important; }

		/* Panel buttons — keep their dedicated colors in dark mode */
		body.pnlrdb-dark .ap773-page .ap773-btn--primary  { background: #6366f1 !important; color: #fff !important; }
		body.pnlrdb-dark .ap773-page .ap773-btn--secondary { background: #1e2535 !important; color: #cbd5e1 !important; border-color: #3d4566 !important; }
		body.pnlrdb-dark .ap773-page .ap773-btn--danger    { background: #3f1111 !important; color: #fca5a5 !important; }

		/* Panel inputs in dark mode */
		body.pnlrdb-dark .ap773-page .ap773-input,
		body.pnlrdb-dark .ap773-page .ap773-select  { background: #1e2535 !important; border-color: #3d4566 !important; color: #e2e8f0 !important; }

		/* Panel toggle labels */
		body.pnlrdb-dark .ap773-page .ap773-toggle-label   { color: #cbd5e1 !important; }
		body.pnlrdb-dark .ap773-page .ap773-checkbox-item  { color: #cbd5e1 !important; }
		body.pnlrdb-dark .ap773-page .ap773-role-card       { background: #1a1f2e !important; border-color: #2d3148 !important; }
		body.pnlrdb-dark .ap773-page .ap773-table thead th  { background: #1e2130 !important; color: #64748b !important; }
		body.pnlrdb-dark .ap773-page .ap773-table tbody td  { color: #e2e8f0 !important; border-color: #1e2535 !important; }
		body.pnlrdb-dark .ap773-page .ap773-form-footer     { background: #0f1117 !important; border-top-color: #2d3148 !important; }
		<?php

		$css = ob_get_clean();
		$css = trim( $css );

		wp_register_style( "pnlrdb-dark-mode", false, array(), PNLRDB_VERSION );
		wp_enqueue_style( "pnlrdb-dark-mode" );
		wp_add_inline_style( "pnlrdb-dark-mode", $css );
	}

	/* -----------------------------------------------------------------------
	   AJAX toggle (triggered by the panel button)
	----------------------------------------------------------------------- */

	public function ajax_toggle() {
		check_ajax_referer( 'pnlrdb_nonce', 'nonce' );
		if ( ! is_user_logged_in() ) { wp_send_json_error( 'Not logged in.' ); }
		$value = ( isset( $_POST['value'] ) && 'on' === $_POST['value'] ) ? 'on' : 'off';
		update_user_meta( get_current_user_id(), self::USER_META_KEY, $value );
		wp_send_json_success( array( 'dark_mode' => $value ) );
	}
}
