<?php
/**
 * Theme Customizer settings.
 *
 * @package HaPantheon
 */

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

/**
 * Sanitize a select value.
 *
 * @param string               $value   Submitted value.
 * @param WP_Customize_Setting $setting Setting instance.
 * @return string
 */
function hapantheon_sanitize_select( $value, $setting ) {
	$control = $setting->manager->get_control( $setting->id );
	if ( $control && isset( $control->choices[ $value ] ) ) {
		return $value;
	}

	return $setting->default;
}

/**
 * Register Customizer controls.
 *
 * @param WP_Customize_Manager $wp_customize Customizer instance.
 */
function hapantheon_customize_register( $wp_customize ) {
	$wp_customize->add_section(
		'hapantheon_layout',
		array(
			'title'       => __( 'HaPantheon — Layout and Fonts', 'hapantheon' ),
			'priority'    => 31,
			'description' => __( 'The imported Hebrew demo defaults to RTL. Change this when building an LTR site.', 'hapantheon' ),
		)
	);

	$wp_customize->add_setting(
		'hapantheon_direction',
		array(
			'default'           => 'rtl',
			'sanitize_callback' => 'hapantheon_sanitize_select',
		)
	);
	$wp_customize->add_control(
		'hapantheon_direction',
		array(
			'type'    => 'select',
			'section' => 'hapantheon_layout',
			'label'   => __( 'Front-end direction', 'hapantheon' ),
			'choices' => array(
				'rtl' => __( 'Right to left (RTL)', 'hapantheon' ),
				'ltr' => __( 'Left to right (LTR)', 'hapantheon' ),
			),
		)
	);

	$wp_customize->add_setting(
		'hapantheon_default_scheme',
		array(
			'default'           => 'system',
			'sanitize_callback' => 'hapantheon_sanitize_select',
		)
	);
	$wp_customize->add_control(
		'hapantheon_default_scheme',
		array(
			'type'    => 'select',
			'section' => 'hapantheon_layout',
			'label'   => __( 'Default color scheme', 'hapantheon' ),
			'choices' => array(
				'system' => __( 'Follow the visitor’s system', 'hapantheon' ),
				'light'  => __( 'Light', 'hapantheon' ),
				'dark'   => __( 'Dark', 'hapantheon' ),
			),
		)
	);

	$wp_customize->add_setting(
		'hapantheon_load_google_fonts',
		array(
			'default'           => false,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'hapantheon_load_google_fonts',
		array(
			'type'        => 'checkbox',
			'section'     => 'hapantheon_layout',
			'label'       => __( 'Optionally load Noto Sans Hebrew, Poppins and Outfit from Google Fonts', 'hapantheon' ),
			'description' => __( 'Disable this to use local/system fallback fonts.', 'hapantheon' ),
		)
	);

	$wp_customize->add_section(
		'hapantheon_colors',
		array(
			'title'       => __( 'HaPantheon — Light Colors', 'hapantheon' ),
			'priority'    => 32,
			'description' => __( 'These controls update the CSS design tokens used across the entire theme.', 'hapantheon' ),
		)
	);

	$light_colors = array(
		'hapantheon_color_bg'       => array( __( 'Page background', 'hapantheon' ), '#F6F1E3' ),
		'hapantheon_color_bg2'      => array( __( 'Secondary background', 'hapantheon' ), '#EFE8D4' ),
		'hapantheon_color_surface'  => array( __( 'Surface / cards', 'hapantheon' ), '#FFFDF6' ),
		'hapantheon_color_surface2' => array( __( 'Secondary surface', 'hapantheon' ), '#F4EEDC' ),
		'hapantheon_color_ink'      => array( __( 'Primary text', 'hapantheon' ), '#1B2126' ),
		'hapantheon_color_ink2'     => array( __( 'Secondary text', 'hapantheon' ), '#3C444C' ),
		'hapantheon_color_muted'    => array( __( 'Muted text', 'hapantheon' ), '#776E56' ),
		'hapantheon_color_gold'     => array( __( 'Gold accent', 'hapantheon' ), '#D19A2B' ),
		'hapantheon_color_gold_hi'  => array( __( 'Gold highlight', 'hapantheon' ), '#F5CE6E' ),
		'hapantheon_color_gold_lo'  => array( __( 'Gold shadow', 'hapantheon' ), '#A87614' ),
		'hapantheon_color_teal'     => array( __( 'Teal accent', 'hapantheon' ), '#0E5F66' ),
		'hapantheon_color_teal_hi'  => array( __( 'Teal highlight', 'hapantheon' ), '#188089' ),
		'hapantheon_color_line'     => array( __( 'Borders and rules', 'hapantheon' ), '#E2D8BC' ),
		'hapantheon_color_night'    => array( __( 'Footer / night background', 'hapantheon' ), '#101418' ),
		'hapantheon_color_night2'   => array( __( 'Secondary night surface', 'hapantheon' ), '#161C22' ),
	);

	foreach ( $light_colors as $setting_id => $definition ) {
		$wp_customize->add_setting(
			$setting_id,
			array(
				'default'           => $definition[1],
				'sanitize_callback' => 'sanitize_hex_color',
			)
		);
		$wp_customize->add_control(
			new WP_Customize_Color_Control(
				$wp_customize,
				$setting_id,
				array(
					'section' => 'hapantheon_colors',
					'label'   => $definition[0],
				)
			)
		);
	}

	$wp_customize->add_section(
		'hapantheon_dark_colors',
		array(
			'title'       => __( 'HaPantheon — Dark Colors', 'hapantheon' ),
			'priority'    => 33,
			'description' => __( 'Dark-mode values used when visitors activate the moon toggle.', 'hapantheon' ),
		)
	);

	$dark_colors = array(
		'hapantheon_dark_bg'       => array( __( 'Dark background', 'hapantheon' ), '#0E1216' ),
		'hapantheon_dark_bg2'      => array( __( 'Dark secondary background', 'hapantheon' ), '#0A0E11' ),
		'hapantheon_dark_surface'  => array( __( 'Dark surface', 'hapantheon' ), '#161C22' ),
		'hapantheon_dark_surface2' => array( __( 'Dark secondary surface', 'hapantheon' ), '#12171C' ),
		'hapantheon_dark_ink'      => array( __( 'Dark primary text', 'hapantheon' ), '#EFE7D2' ),
		'hapantheon_dark_ink2'     => array( __( 'Dark secondary text', 'hapantheon' ), '#CEC5AC' ),
		'hapantheon_dark_muted'    => array( __( 'Dark muted text', 'hapantheon' ), '#9C9278' ),
		'hapantheon_dark_gold'     => array( __( 'Dark gold', 'hapantheon' ), '#D9B45C' ),
		'hapantheon_dark_gold_hi'  => array( __( 'Dark gold highlight', 'hapantheon' ), '#F2DFA0' ),
		'hapantheon_dark_gold_lo'  => array( __( 'Dark gold shadow', 'hapantheon' ), '#B8912F' ),
		'hapantheon_dark_teal'     => array( __( 'Dark teal', 'hapantheon' ), '#4CC5CC' ),
		'hapantheon_dark_teal_hi'  => array( __( 'Dark teal highlight', 'hapantheon' ), '#7ADCE1' ),
		'hapantheon_dark_line'     => array( __( 'Dark borders', 'hapantheon' ), '#262E35' ),
	);

	foreach ( $dark_colors as $setting_id => $definition ) {
		$wp_customize->add_setting(
			$setting_id,
			array(
				'default'           => $definition[1],
				'sanitize_callback' => 'sanitize_hex_color',
			)
		);
		$wp_customize->add_control(
			new WP_Customize_Color_Control(
				$wp_customize,
				$setting_id,
				array(
					'section' => 'hapantheon_dark_colors',
					'label'   => $definition[0],
				)
			)
		);
	}

	$wp_customize->add_section(
		'hapantheon_footer',
		array(
			'title'       => __( 'HaPantheon — Footer', 'hapantheon' ),
			'priority'    => 34,
			'description' => __( 'Footer navigation and legal links are managed under Appearance → Menus. The controls below manage headings, branding and social links.', 'hapantheon' ),
		)
	);

	$wp_customize->add_setting(
		'hapantheon_footer_about',
		array(
			'default'           => 'ארכיון דיגיטלי אצור לחוויות איכות, תוכן נדיר ומחשבה. סובייקטיבי, ובכוונה.',
			'sanitize_callback' => 'sanitize_textarea_field',
		)
	);
	$wp_customize->add_control(
		'hapantheon_footer_about',
		array(
			'type'    => 'textarea',
			'section' => 'hapantheon_footer',
			'label'   => __( 'Footer description', 'hapantheon' ),
		)
	);

	$footer_headings = array(
		'hapantheon_footer_navigation_title'  => array( __( 'Navigation-column heading', 'hapantheon' ), 'ניווט' ),
		'hapantheon_footer_halls_title'       => array( __( 'Halls-column heading', 'hapantheon' ), 'האולמות' ),
		'hapantheon_footer_information_title' => array( __( 'Information-column heading', 'hapantheon' ), 'מידע' ),
		'hapantheon_footer_newsletter_title'  => array( __( 'Newsletter heading', 'hapantheon' ), 'המגילה החודשית' ),
	);

	foreach ( $footer_headings as $setting_id => $definition ) {
		$wp_customize->add_setting(
			$setting_id,
			array(
				'default'           => $definition[1],
				'sanitize_callback' => 'sanitize_text_field',
			)
		);
		$wp_customize->add_control(
			$setting_id,
			array(
				'type'    => 'text',
				'section' => 'hapantheon_footer',
				'label'   => $definition[0],
			)
		);
	}

	$wp_customize->add_setting(
		'hapantheon_footer_copyright',
		array(
			'default'           => 'נבנה באבן, קוד ושיקול דעת.',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'hapantheon_footer_copyright',
		array(
			'type'    => 'text',
			'section' => 'hapantheon_footer',
			'label'   => __( 'Footer closing line', 'hapantheon' ),
		)
	);

	$social_controls = array(
		'hapantheon_instagram_url' => array( __( 'Instagram URL', 'hapantheon' ), 'url', '' ),
		'hapantheon_facebook_url'  => array( __( 'Facebook URL', 'hapantheon' ), 'url', '' ),
		'hapantheon_youtube_url'   => array( __( 'YouTube URL', 'hapantheon' ), 'url', '' ),
		'hapantheon_contact_email' => array( __( 'Public email address', 'hapantheon' ), 'email', get_option( 'admin_email' ) ),
	);

	foreach ( $social_controls as $setting_id => $definition ) {
		$sanitize = 'email' === $definition[1] ? 'sanitize_email' : 'esc_url_raw';
		$wp_customize->add_setting(
			$setting_id,
			array(
				'default'           => $definition[2],
				'sanitize_callback' => $sanitize,
			)
		);
		$wp_customize->add_control(
			$setting_id,
			array(
				'type'    => $definition[1],
				'section' => 'hapantheon_footer',
				'label'   => $definition[0],
			)
		);
	}
}
add_action( 'customize_register', 'hapantheon_customize_register' );

/**
 * Read a valid Customizer color with a fallback.
 *
 * @param string $setting Setting ID.
 * @param string $default Default hex.
 * @return string
 */
function hapantheon_theme_color( $setting, $default ) {
	$value = sanitize_hex_color( get_theme_mod( $setting, $default ) );
	return $value ? $value : $default;
}

/**
 * Build CSS variables from Customizer values.
 *
 * @return string
 */
function hapantheon_get_customizer_css() {
	$light = array(
		'--bg'       => hapantheon_theme_color( 'hapantheon_color_bg', '#F6F1E3' ),
		'--bg2'      => hapantheon_theme_color( 'hapantheon_color_bg2', '#EFE8D4' ),
		'--srf'      => hapantheon_theme_color( 'hapantheon_color_surface', '#FFFDF6' ),
		'--srf2'     => hapantheon_theme_color( 'hapantheon_color_surface2', '#F4EEDC' ),
		'--ink'      => hapantheon_theme_color( 'hapantheon_color_ink', '#1B2126' ),
		'--ink2'     => hapantheon_theme_color( 'hapantheon_color_ink2', '#3C444C' ),
		'--mut'      => hapantheon_theme_color( 'hapantheon_color_muted', '#776E56' ),
		'--gold'     => hapantheon_theme_color( 'hapantheon_color_gold', '#D19A2B' ),
		'--gold-hi'  => hapantheon_theme_color( 'hapantheon_color_gold_hi', '#F5CE6E' ),
		'--gold-lo'  => hapantheon_theme_color( 'hapantheon_color_gold_lo', '#A87614' ),
		'--gold-ink' => hapantheon_theme_color( 'hapantheon_color_gold_lo', '#96700F' ),
		'--teal'     => hapantheon_theme_color( 'hapantheon_color_teal', '#0E5F66' ),
		'--teal-hi'  => hapantheon_theme_color( 'hapantheon_color_teal_hi', '#188089' ),
		'--line'     => hapantheon_theme_color( 'hapantheon_color_line', '#E2D8BC' ),
		'--night'    => hapantheon_theme_color( 'hapantheon_color_night', '#101418' ),
		'--night-2'  => hapantheon_theme_color( 'hapantheon_color_night2', '#161C22' ),
	);

	$dark = array(
		'--bg'      => hapantheon_theme_color( 'hapantheon_dark_bg', '#0E1216' ),
		'--bg2'     => hapantheon_theme_color( 'hapantheon_dark_bg2', '#0A0E11' ),
		'--srf'     => hapantheon_theme_color( 'hapantheon_dark_surface', '#161C22' ),
		'--srf2'    => hapantheon_theme_color( 'hapantheon_dark_surface2', '#12171C' ),
		'--ink'     => hapantheon_theme_color( 'hapantheon_dark_ink', '#EFE7D2' ),
		'--ink2'    => hapantheon_theme_color( 'hapantheon_dark_ink2', '#CEC5AC' ),
		'--mut'     => hapantheon_theme_color( 'hapantheon_dark_muted', '#9C9278' ),
		'--gold'     => hapantheon_theme_color( 'hapantheon_dark_gold', '#D9B45C' ),
		'--gold-hi'  => hapantheon_theme_color( 'hapantheon_dark_gold_hi', '#F2DFA0' ),
		'--gold-lo'  => hapantheon_theme_color( 'hapantheon_dark_gold_lo', '#B8912F' ),
		'--gold-ink' => hapantheon_theme_color( 'hapantheon_dark_gold_lo', '#B8912F' ),
		'--teal'     => hapantheon_theme_color( 'hapantheon_dark_teal', '#4CC5CC' ),
		'--teal-hi'  => hapantheon_theme_color( 'hapantheon_dark_teal_hi', '#7ADCE1' ),
		'--line'     => hapantheon_theme_color( 'hapantheon_dark_line', '#262E35' ),
	);

	$css = ':root{';
	foreach ( $light as $property => $value ) {
		$css .= $property . ':' . $value . ';';
	}
	$css .= '--grad-gold:linear-gradient(115deg,' . $light['--gold-lo'] . ' 0%,' . $light['--gold'] . ' 30%,' . $light['--gold-hi'] . ' 50%,' . $light['--gold'] . ' 70%,' . $light['--gold-lo'] . ' 100%);';
	$css .= '}html[data-theme="dark"]{';
	foreach ( $dark as $property => $value ) {
		$css .= $property . ':' . $value . ';';
	}
	$css .= '--grad-gold:linear-gradient(115deg,' . $dark['--gold-lo'] . ' 0%,' . $dark['--gold'] . ' 30%,' . $dark['--gold-hi'] . ' 50%,' . $dark['--gold'] . ' 70%,' . $dark['--gold-lo'] . ' 100%);';
	$css .= '}';

	return $css;
}
