<?php
/**
 * Maps country / region codes to a consent preset.
 *
 * @package BlaminhorEssentials
 */

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

/**
 * A "preset" encodes how a banner should behave for a given jurisdiction:
 * which model (opt-in / opt-out / notice), which categories are pre-checked,
 * whether the "Do Not Sell or Share" CCPA link should be visible, whether
 * a Global Privacy Control signal triggers automatic refusal, etc.
 */
class Blaminhor_Essentials_Cookies_Regional_Presets {

	/**
	 * Country -> preset key.
	 *
	 * @return array<string,string>
	 */
	public function get_country_mapping() {
		$map = array(
			// EU-27 (opt-in strict).
			'AT' => 'eu_strict', 'BE' => 'eu_strict', 'BG' => 'eu_strict',
			'CY' => 'eu_strict', 'CZ' => 'eu_strict', 'DE' => 'de_strict',
			'DK' => 'eu_strict', 'EE' => 'eu_strict', 'ES' => 'eu_strict',
			'FI' => 'eu_strict', 'FR' => 'fr_strict', 'GR' => 'eu_strict',
			'HR' => 'eu_strict', 'HU' => 'eu_strict', 'IE' => 'eu_strict',
			'IT' => 'eu_strict', 'LT' => 'eu_strict', 'LU' => 'eu_strict',
			'LV' => 'eu_strict', 'MT' => 'eu_strict', 'NL' => 'eu_strict',
			'PL' => 'eu_strict', 'PT' => 'eu_strict', 'RO' => 'eu_strict',
			'SE' => 'eu_strict', 'SI' => 'eu_strict', 'SK' => 'eu_strict',

			// EEA + Switzerland.
			'NO' => 'eu_strict', 'IS' => 'eu_strict', 'LI' => 'eu_strict',
			'CH' => 'ch_opt_out_info',

			// UK (PECR with DUAA 2025 exemptions).
			'GB' => 'uk_opt_in_with_exemptions',

			// North America.
			'US' => 'us_optout_with_gpc',
			'CA' => 'ca_hybrid',

			// LATAM.
			'BR' => 'br_lgpd_optin', 'AR' => 'ar_opt_in', 'CL' => 'cl_opt_in',
			'MX' => 'mx_hybrid', 'CO' => 'latam_default', 'PE' => 'latam_default',

			// APAC.
			'JP' => 'jp_opt_out', 'KR' => 'kr_strict_optin', 'CN' => 'cn_strict_optin',
			'IN' => 'in_dpdp_optin', 'SG' => 'sg_hybrid', 'TH' => 'th_opt_in',
			'AU' => 'au_hybrid', 'NZ' => 'nz_notice', 'ID' => 'id_opt_in',

			// MEA.
			'ZA' => 'za_popia_optin', 'TR' => 'tr_kvkk_optin', 'IL' => 'il_opt_in',
			'RU' => 'ru_consent_local', 'AE' => 'ae_hybrid', 'EG' => 'eg_hybrid',
		);

		return (array) apply_filters( 'blaminhor_cookies_country_mapping', $map );
	}

	/**
	 * Definition of every named preset.
	 *
	 * @return array<string,array>
	 */
	public function get_presets() {
		$opt_in_strict = array(
			'model'                => 'opt_in',
			'prechecked'           => array( 'essential' ),
			'show_reject_all'      => true,
			'reject_equal_to_accept' => true,
			'dnsmpi_visible'       => false,
			'gpc_respect'          => false,
			'reconsent_months'     => 6,
		);
		$opt_out = array(
			'model'                => 'opt_out',
			'prechecked'           => array( 'essential', 'preferences', 'statistics', 'marketing' ),
			'show_reject_all'      => true,
			'reject_equal_to_accept' => true,
			'dnsmpi_visible'       => false,
			'gpc_respect'          => false,
			'reconsent_months'     => 12,
		);
		$notice = array(
			'model'                => 'notice',
			'prechecked'           => array( 'essential', 'preferences', 'statistics', 'marketing' ),
			'show_reject_all'      => false,
			'reject_equal_to_accept' => false,
			'dnsmpi_visible'       => false,
			'gpc_respect'          => false,
			'reconsent_months'     => 24,
		);

		$presets = array(
			'eu_strict'                  => $opt_in_strict,
			'fr_strict'                  => array_merge( $opt_in_strict, array( 'show_reject_one_click' => true ) ),
			'de_strict'                  => array_merge( $opt_in_strict, array( 'impressum_required' => true ) ),
			'ch_opt_out_info'            => $opt_out,
			'uk_opt_in_with_exemptions'  => array_merge( $opt_in_strict, array( 'analytics_first_party_aggregated_exempt' => true ) ),

			'us_optout_with_gpc'         => array_merge( $opt_out, array( 'dnsmpi_visible' => true, 'gpc_respect' => true ) ),
			'ca_hybrid'                  => array_merge( $opt_out, array( 'quebec_opt_in' => true ) ),

			'br_lgpd_optin'              => $opt_in_strict,
			'ar_opt_in'                  => $opt_in_strict,
			'cl_opt_in'                  => $opt_in_strict,
			'mx_hybrid'                  => $opt_out,
			'latam_default'              => $opt_in_strict,

			'jp_opt_out'                 => $opt_out,
			'kr_strict_optin'            => $opt_in_strict,
			'cn_strict_optin'            => $opt_in_strict,
			'in_dpdp_optin'              => $opt_in_strict,
			'sg_hybrid'                  => $opt_out,
			'th_opt_in'                  => $opt_in_strict,
			'au_hybrid'                  => $opt_out,
			'nz_notice'                  => $notice,
			'id_opt_in'                  => $opt_in_strict,

			'za_popia_optin'             => $opt_in_strict,
			'tr_kvkk_optin'              => $opt_in_strict,
			'il_opt_in'                  => $opt_in_strict,
			'ru_consent_local'           => $opt_in_strict,
			'ae_hybrid'                  => $opt_out,
			'eg_hybrid'                  => $opt_out,

			// Final fallback (most protective).
			'default'                    => $opt_in_strict,
		);

		return (array) apply_filters( 'blaminhor_cookies_presets', $presets );
	}

	/**
	 * Resolve the preset definition for a given country code.
	 *
	 * @param string|null $country ISO-3166 alpha-2 or null.
	 * @return array
	 */
	public function resolve_preset( $country ) {
		$presets = $this->get_presets();
		if ( null === $country ) {
			return array_merge( array( 'key' => 'default' ), $presets['default'] );
		}
		$mapping  = $this->get_country_mapping();
		$key      = isset( $mapping[ $country ] ) && isset( $presets[ $mapping[ $country ] ] ) ? $mapping[ $country ] : 'default';
		return array_merge( array( 'key' => $key ), $presets[ $key ] );
	}

	/**
	 * Return the human-readable name of each preset (admin UI).
	 *
	 * @return array<string,string>
	 */
	public function get_preset_labels() {
		return array(
			'eu_strict'                 => __( 'European Union — strict opt-in', 'blaminhor-essentials' ),
			'fr_strict'                 => __( 'France — strict opt-in + 1-click reject', 'blaminhor-essentials' ),
			'de_strict'                 => __( 'Germany — strict opt-in (TDDDG)', 'blaminhor-essentials' ),
			'ch_opt_out_info'           => __( 'Switzerland — informed opt-out (nFADP)', 'blaminhor-essentials' ),
			'uk_opt_in_with_exemptions' => __( 'United Kingdom — opt-in with PECR exemptions', 'blaminhor-essentials' ),
			'us_optout_with_gpc'        => __( 'United States — opt-out + GPC + Do Not Sell or Share', 'blaminhor-essentials' ),
			'ca_hybrid'                 => __( 'Canada — hybrid (Quebec opt-in, rest of Canada opt-out)', 'blaminhor-essentials' ),
			'br_lgpd_optin'             => __( 'Brazil — opt-in (LGPD)', 'blaminhor-essentials' ),
			'jp_opt_out'                => __( 'Japan — opt-out (APPI)', 'blaminhor-essentials' ),
			'kr_strict_optin'           => __( 'South Korea — strict opt-in (PIPA)', 'blaminhor-essentials' ),
			'cn_strict_optin'           => __( 'China — strict opt-in (PIPL)', 'blaminhor-essentials' ),
			'in_dpdp_optin'             => __( 'India — opt-in (DPDP Act)', 'blaminhor-essentials' ),
			'au_hybrid'                 => __( 'Australia — hybrid (Privacy Act)', 'blaminhor-essentials' ),
			'za_popia_optin'            => __( 'South Africa — opt-in (POPIA)', 'blaminhor-essentials' ),
			'tr_kvkk_optin'             => __( 'Turkey — opt-in (KVKK)', 'blaminhor-essentials' ),
			'default'                   => __( 'Default — strictest opt-in (safe fallback)', 'blaminhor-essentials' ),
		);
	}
}
