<?php
/**
 * Eshraq Portfolio Customizer Settings
 *
 * @package Eshraq_Portfolio
 * @since 1.0.0
 */

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

/**
 * Register Customizer settings, sections, and controls.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function eshraq_customize_register( $wp_customize ) {

	// ─── Panel: Eshraq Portfolio ──────────────────────────────
	$wp_customize->add_panel( 'eshraq_panel', array(
		'title'       => esc_html__( 'Eshraq Portfolio', 'eshraq-portfolio' ),
		'description' => esc_html__( 'Customize the Eshraq Portfolio theme.', 'eshraq-portfolio' ),
		'priority'    => 30,
	) );

	// ─── Section: General ─────────────────────────────────────
	$wp_customize->add_section( 'eshraq_general', array(
		'title' => esc_html__( 'General Settings', 'eshraq-portfolio' ),
		'panel' => 'eshraq_panel',
	) );

	// Site Tagline.
	$wp_customize->add_setting( 'eshraq_tagline', array(
		'default'           => 'Artist & CEO',
		'sanitize_callback' => 'eshraq_sanitize_text',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_tagline', array(
		'label'   => esc_html__( 'Site Tagline', 'eshraq-portfolio' ),
		'section' => 'eshraq_general',
		'type'    => 'text',
	) );

	// Hero Title.
	$wp_customize->add_setting( 'eshraq_hero_title', array(
		'default'           => 'Photography & Visual Arts',
		'sanitize_callback' => 'eshraq_sanitize_text',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_hero_title', array(
		'label'   => esc_html__( 'Hero Title', 'eshraq-portfolio' ),
		'section' => 'eshraq_general',
		'type'    => 'text',
	) );

	// Hero Subtitle.
	(wp_customize()->add_setting( 'eshraq_hero_subtitle', array(
		'default'           => 'Delivering fashion and beauty artistically in portraits. Based in Los Angeles.',
		'sanitize_callback' => 'eshraq_sanitize_textarea',
		'transport'         => 'postMessage',
	) ) );
	$wp_customize->add_control( 'eshraq_hero_subtitle', array(
		'label'   => esc_html__( 'Hero Subtitle', 'eshraq-portfolio' ),
		'section' => 'eshraq_general',
		'type'    => 'textarea',
	) );

	// About Page Bio.
	$wp_customize->add_setting( 'eshraq_about_bio', array(
		'default'           => 'Delivering high fashion and beauty artistically, while providing excellent personal attention and demonstrating a unique experience for each and every client.',
		'sanitize_callback' => 'eshraq_sanitize_textarea',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_about_bio', array(
		'label'   => esc_html__( 'About Bio', 'eshraq-portfolio' ),
		'section' => 'eshraq_general',
		'type'    => 'textarea',
	) );

	// CTA Button Text.
	$wp_customize->add_setting( 'eshraq_cta_text', array(
		'default'           => 'Book a Session',
		'sanitize_callback' => 'eshraq_sanitize_text',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_cta_text', array(
		'label'   => esc_html__( 'CTA Button Text', 'eshraq-portfolio' ),
		'section' => 'eshraq_general',
		'type'    => 'text',
	) );

	// CTA Button URL.
	$wp_customize->add_setting( 'eshraq_cta_url', array(
		'default'           => '#contact',
		'sanitize_callback' => 'esc_url_raw',
	) );
	$wp_customize->add_control( 'eshraq_cta_url', array(
		'label'   => esc_html__( 'CTA Button URL', 'eshraq-portfolio' ),
		'section' => 'eshraq_general',
		'type'    => 'url',
	) );

	// ─── Section: Colors ──────────────────────────────────────
	$wp_customize->add_section( 'eshraq_colors', array(
		'title'    => esc_html__( 'Theme Colors', 'eshraq-portfolio' ),
		'panel'    => 'eshraq_panel',
		'priority' => 20,
	) );

	// Primary Color.
	$wp_customize->add_setting( 'eshraq_primary_color', array(
		'default'           => '#0a0a0a',
		'sanitize_callback' => 'eshraq_sanitize_color',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'eshraq_primary_color', array(
		'label'   => esc_html__( 'Primary Color', 'eshraq-portfolio' ),
		'section' => 'eshraq_colors',
	) ) );

	// Background Color.
	$wp_customize->add_setting( 'eshraq_bg_color', array(
		'default'           => '#f8f6f1',
		'sanitize_callback' => 'eshraq_sanitize_color',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'eshraq_bg_color', array(
		'label'   => esc_html__( 'Background Color', 'eshraq-portfolio' ),
		'section' => 'eshraq_colors',
	) ) );

	// Accent Color.
	$wp_customize->add_setting( 'eshraq_accent_color', array(
		'default'           => '#1a1a1a',
		'sanitize_callback' => 'eshraq_sanitize_color',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'eshraq_accent_color', array(
		'label'   => esc_html__( 'Accent Color', 'eshraq-portfolio' ),
		'section' => 'eshraq_colors',
	) ) );

	// Muted Color.
	$wp_customize->add_setting( 'eshraq_muted_color', array(
		'default'           => '#999999',
		'sanitize_callback' => 'eshraq_sanitize_color',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'eshraq_muted_color', array(
		'label'   => esc_html__( 'Muted Text Color', 'eshraq-portfolio' ),
		'section' => 'eshraq_colors',
	) ) );

	// ─── Section: Typography ──────────────────────────────────
	$wp_customize->add_section( 'eshraq_typography', array(
		'title'    => esc_html__( 'Typography', 'eshraq-portfolio' ),
		'panel'    => 'eshraq_panel',
		'priority' => 30,
	) );

	// Heading Font.
	$wp_customize->add_setting( 'eshraq_heading_font', array(
		'default'           => 'Playfair Display',
		'sanitize_callback' => 'eshraq_sanitize_text',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_heading_font', array(
		'label'   => esc_html__( 'Heading Font', 'eshraq-portfolio' ),
		'section' => 'eshraq_typography',
		'type'    => 'select',
		'choices' => array(
			'Playfair Display' => 'Playfair Display',
			'DM Sans'          => 'DM Sans',
			'Georgia'          => 'Georgia',
			'Arial'            => 'Arial',
		),
	) );

	// Body Font.
	$wp_customize->add_setting( 'eshraq_body_font', array(
		'default'           => 'DM Sans',
		'sanitize_callback' => 'eshraq_sanitize_text',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_body_font', array(
		'label'   => esc_html__( 'Body Font', 'eshraq-portfolio' ),
		'section' => 'eshraq_typography',
		'type'    => 'select',
		'choices' => array(
			'DM Sans'          => 'DM Sans',
			'Playfair Display' => 'Playfair Display',
			'Georgia'          => 'Georgia',
			'Arial'            => 'Arial',
		),
	) );

	// Base Font Size.
	$wp_customize->add_setting( 'eshraq_base_font_size', array(
		'default'           => '16',
		'sanitize_callback' => 'absint',
		'transport'         => 'postMessage',
	) );
	$wp_customize->add_control( 'eshraq_base_font_size', array(
		'label'       => esc_html__( 'Base Font Size (px)', 'eshraq-portfolio' ),
		'section'     => 'eshraq_typography',
		'type'        => 'range',
		'input_attrs' => array(
			'min'  => 12,
			'max'  => 24,
			'step' => 1,
		),
	) );

	// ─── Section: Footer ──────────────────────────────────────
	$wp_customize->add_section( 'eshraq_footer', array(
		'title'    => esc_html__( 'Footer Settings', 'eshraq-portfolio' ),
		'panel'    => 'eshraq_panel',
		'priority' => 40,
	) );

	// Copyright Text.
	$wp_customize->add_setting( 'eshraq_copyright', array(
		'default'           => '',
		'sanitize_callback' => 'eshraq_sanitize_text',
	) );
	$wp_customize->add_control( 'eshraq_copyright', array(
		'label'   => esc_html__( 'Copyright Text', 'eshraq-portfolio' ),
		'section' => 'eshraq_footer',
		'type'    => 'text',
	) );

	// Credit Link.
	$wp_customize->add_setting( 'eshraq_credit_text', array(
		'default'           => 'Theme by',
		'sanitize_callback' => 'eshraq_sanitize_text',
	) );
	$wp_customize->add_control( 'eshraq_credit_text', array(
		'label'   => esc_html__( 'Credit Text', 'eshraq-portfolio' ),
		'section' => 'eshraq_footer',
		'type'    => 'text',
	) );

	// Credit URL.
	$wp_customize->add_setting( 'eshraq_credit_url', array(
		'default'           => 'https://eshraq.dev',
		'sanitize_callback' => 'esc_url_raw',
	) );
	$wp_customize->add_control( 'eshraq_credit_url', array(
		'label'   => esc_html__( 'Credit URL', 'eshraq-portfolio' ),
		'section' => 'eshraq_footer',
		'type'    => 'url',
	) );

	// Credit Name.
	$wp_customize->add_setting( 'eshraq_credit_name', array(
		'default'           => 'Eshraq',
		'sanitize_callback' => 'eshraq_sanitize_text',
	) );
	$wp_customize->add_control( 'eshraq_credit_name', array(
		'label'   => esc_html__( 'Credit Name', 'eshraq-portfolio' ),
		'section' => 'eshraq_footer',
		'type'    => 'text',
	) );

	// ─── Section: Social Links ────────────────────────────────
	$wp_customize->add_section( 'eshraq_social', array(
		'title'    => esc_html__( 'Social Links', 'eshraq-portfolio' ),
		'panel'    => 'eshraq_panel',
		'priority' => 50,
	) );

	$social_links = array(
		'email'   => esc_html__( 'Email Address', 'eshraq-portfolio' ),
		'instagram' => esc_html__( 'Instagram URL', 'eshraq-portfolio' ),
		'twitter' => esc_html__( 'Twitter/X URL', 'eshraq-portfolio' ),
		'facebook' => esc_html__( 'Facebook URL', 'eshraq-portfolio' ),
		'youtube' => esc_html__( 'YouTube URL', 'eshraq-portfolio' ),
		'vimeo'   => esc_html__( 'Vimeo URL', 'eshraq-portfolio' ),
		'linkedin' => esc_html__( 'LinkedIn URL', 'eshraq-portfolio' ),
		'tiktok'  => esc_html__( 'TikTok URL', 'eshraq-portfolio' ),
	);

	foreach ( $social_links as $key => $label ) {
		$default = ( 'email' === $key ) ? 'mailto:hello@example.com' : '';
		$wp_customize->add_setting( 'eshraq_social_' . $key, array(
			'default'           => $default,
			'sanitize_callback' => ( 'email' === $key ) ? 'sanitize_email' : 'esc_url_raw',
		) );
		$wp_customize->add_control( 'eshraq_social_' . $key, array(
			'label'   => $label,
			'section' => 'eshraq_social',
			'type'    => 'url',
		) );
	}

	// Selective refresh for site title.
	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial( 'blogname', array(
			'selector'        => '.site-logo-text',
			'render_callback' => function () {
			 bloginfo( 'name' );
			},
		) );
	}
}
add_action( 'customize_register', 'eshraq_customize_register' );

/**
 * Output custom CSS from Customizer settings.
 */
function eshraq_customizer_css() {
	$primary_color = get_theme_mod( 'eshraq_primary_color', '#0a0a0a' );
	$bg_color      = get_theme_mod( 'eshraq_bg_color', '#f8f6f1' );
	$accent_color  = get_theme_mod( 'eshraq_accent_color', '#1a1a1a' );
	$muted_color   = get_theme_mod( 'eshraq_muted_color', '#999999' );
	$heading_font  = get_theme_mod( 'eshraq_heading_font', 'Playfair Display' );
	$body_font     = get_theme_mod( 'eshraq_body_font', 'DM Sans' );
	$base_size     = get_theme_mod( 'eshraq_base_font_size', '16' );

	$css = sprintf(
		':root {
			--eshraq-primary: %s;
			--eshraq-bg: %s;
			--eshraq-accent: %s;
			--eshraq-muted: %s;
			--eshraq-heading-font: "%s", Georgia, serif;
			--eshraq-body-font: "%s", -apple-system, BlinkMacSystemFont, sans-serif;
			--eshraq-base-size: %spx;
		}',
		esc_attr( $primary_color ),
		esc_attr( $bg_color ),
		esc_attr( $accent_color ),
		esc_attr( $muted_color ),
		esc_attr( $heading_font ),
		esc_attr( $body_font ),
		esc_attr( $base_size )
	);

	// Only output if colors differ from defaults.
	if ( '#0a0a0a' !== $primary_color || '#f8f6f1' !== $bg_color || '#1a1a1a' !== $accent_color || '#999999' !== $muted_color || 'Playfair Display' !== $heading_font || 'DM Sans' !== $body_font || '16' !== $base_size ) {
		printf( '<style id="eshraq-customizer-css">%s</style>' . "\n", esc_html( $css ) );
	}
}
add_action( 'wp_head', 'eshraq_customizer_css', 99 );

/**
 * Enqueue Customizer preview script.
 */
function eshraq_customize_preview_js() {
	wp_enqueue_script(
		'eshraq-customizer',
		ESHRAQ_URI . '/assets/js/customizer.js',
		array( 'customize-preview' ),
		ESHRAQ_VERSION,
		true
	);
}
add_action( 'customize_preview_init', 'eshraq_customize_preview_js' );
