<?php
/**
 * One-click branding and dynamic CSS.
 *
 * @package EasySite_Pro
 */

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

/**
 * Apply design preset theme mods.
 *
 * @param string $preset Preset key.
 */
function easysite_apply_design_preset( $preset ) {
	$presets = easysite_get_design_presets();
	if ( ! isset( $presets[ $preset ] ) ) {
		return;
	}

	$data = $presets[ $preset ];
	set_theme_mod( 'easysite_design_preset', $preset );
	set_theme_mod( 'easysite_main_color', $data['main_color'] );
	set_theme_mod( 'easysite_accent_color', $data['accent_color'] );
	set_theme_mod( 'easysite_font_pair', $data['font_pair'] );
	easysite_apply_preset_gradients( $preset );
}

/**
 * Apply one-click branding across theme mods.
 */
function easysite_apply_one_click_branding() {
	$main   = easysite_get_mod( 'easysite_main_color', '#2563eb' );
	$accent = easysite_get_mod( 'easysite_accent_color', '#0ea5e9' );
	$pair   = easysite_get_mod( 'easysite_font_pair', 'inter-system' );

	set_theme_mod( 'easysite_branding_applied', time() );
	set_theme_mod( 'easysite_button_color', $main );
	set_theme_mod( 'easysite_link_color', $main );
	set_theme_mod( 'easysite_heading_color', '#111827' );

	do_action( 'easysite_branding_applied', $main, $accent, $pair );
}

/**
 * Enqueue Google Fonts for selected pair.
 */
function easysite_enqueue_fonts() {
	$pair_key = easysite_get_mod( 'easysite_font_pair', 'inter-system' );
	$pairs    = easysite_get_font_pairs();

	if ( ! isset( $pairs[ $pair_key ] ) || empty( $pairs[ $pair_key ]['google'] ) ) {
		return;
	}

	wp_enqueue_style(
		'easysite-google-fonts',
		'https://fonts.googleapis.com/css2?family=' . rawurlencode( str_replace( '|', '&family=', $pairs[ $pair_key ]['google'] ) ) . '&display=swap',
		array(),
		null
	);
}
add_action( 'wp_enqueue_scripts', 'easysite_enqueue_fonts', 5 );

/**
 * Output inline branding CSS variables.
 */
function easysite_output_branding_css() {
	$main    = easysite_get_mod( 'easysite_main_color', '#2563eb' );
	$accent  = easysite_get_mod( 'easysite_accent_color', '#0ea5e9' );
	$pair    = easysite_get_mod( 'easysite_font_pair', 'inter-system' );
	$pairs   = easysite_get_font_pairs();
	$heading = isset( $pairs[ $pair ] ) ? $pairs[ $pair ]['heading'] : 'sans-serif';
	$body    = isset( $pairs[ $pair ] ) ? $pairs[ $pair ]['body'] : 'sans-serif';
	$radius  = easysite_get_mod( 'easysite_rounded_corners', 'large' );
	$radius_map = array( 'large' => '16px', 'medium' => '10px', 'sharp' => '4px' );
	$r = isset( $radius_map[ $radius ] ) ? $radius_map[ $radius ] : '16px';

	$use_gradients = easysite_get_mod( 'easysite_use_gradients', true );
	$gradient      = easysite_resolve_gradient();

	$css = sprintf(
		':root{--easysite-main:%1$s;--easysite-accent:%2$s;--easysite-heading-font:%3$s;--easysite-body-font:%4$s;--easysite-radius:%5$s;',
		esc_attr( $main ),
		esc_attr( $accent ),
		esc_attr( $heading ),
		esc_attr( $body ),
		esc_attr( $r )
	);

	if ( $use_gradients ) {
		$css .= sprintf(
			'--easysite-gradient:%1$s;--easysite-gradient-from:%2$s;--easysite-gradient-to:%3$s;--easysite-gradient-soft:linear-gradient(135deg, %2$s22, %3$s22);',
			$gradient['css'],
			esc_attr( $gradient['from'] ),
			esc_attr( $gradient['to'] )
		);
	} else {
		$css .= sprintf(
			'--easysite-gradient:linear-gradient(135deg, %1$s, %2$s);--easysite-gradient-from:%1$s;--easysite-gradient-to:%2$s;--easysite-gradient-soft:linear-gradient(135deg, %1$s22, %2$s22);',
			esc_attr( $main ),
			esc_attr( $accent )
		);
	}

	$css .= '}';

	wp_add_inline_style( 'easysite-pro-main', $css );
}

/**
 * AJAX: Apply branding from customizer/admin.
 */
function easysite_ajax_apply_branding() {
	check_ajax_referer( 'easysite_admin_nonce', 'nonce' );

	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_send_json_error( array( 'message' => __( 'Permission denied.', 'easysite-pro' ) ) );
	}

	easysite_apply_one_click_branding();
	wp_send_json_success( array( 'message' => __( 'Branding applied across your site!', 'easysite-pro' ) ) );
}
add_action( 'wp_ajax_easysite_apply_branding', 'easysite_ajax_apply_branding' );
