<?php

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

class AIEO_PII_Hasher {

	
	public static function normalize_and_hash( $key, $value ) {
		$norm = self::normalize( $key, (string) $value );
		if ( '' === $norm ) {
			return '';
		}
		return hash( 'sha256', $norm );
	}

	
	public static function normalize( $key, $value ) {
		$value = (string) $value;

		switch ( $key ) {
			case 'em':
				return self::lc( $value );

			case 'ph':
				
				
				
				return preg_replace( '/[^0-9]/', '', $value );

			case 'fn':
			case 'ln':
			case 'ct':
			case 'st':
				
				$v = self::lc( $value );
				$v = preg_replace( '/[\s\.\-_,]+/u', '', $v );
				return (string) $v;

			case 'zp':
				
				return self::lc( preg_replace( '/\s+/', '', $value ) );

			case 'country':
				
				return self::lc( $value );

			case 'external_id':
				
				return trim( $value );

			default:
				return self::lc( $value );
		}
	}

	
	public static function lc( $v ) {
		$v = trim( (string) $v );
		return function_exists( 'mb_strtolower' ) ? mb_strtolower( $v, 'UTF-8' ) : strtolower( $v );
	}

	
	public static function google_gtag_user_data( $order ) {
		$ud = array();
		$em = self::normalize_and_hash( 'em', (string) $order->get_billing_email() );
		$ph = self::normalize_and_hash( 'ph', (string) $order->get_billing_phone() );
		if ( '' !== $em ) { $ud['sha256_email_address'] = $em; }
		if ( '' !== $ph ) { $ud['sha256_phone_number'] = $ph; }

		$addr   = array();
		$fn     = self::normalize_and_hash( 'fn', (string) $order->get_billing_first_name() );
		$ln     = self::normalize_and_hash( 'ln', (string) $order->get_billing_last_name() );
		$street = self::normalize_and_hash( 'street', trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ) );
		if ( '' !== $fn )     { $addr['sha256_first_name'] = $fn; }
		if ( '' !== $ln )     { $addr['sha256_last_name'] = $ln; }
		if ( '' !== $street ) { $addr['sha256_street'] = $street; }
		$city    = self::lc( $order->get_billing_city() );
		$region  = self::lc( $order->get_billing_state() );
		$postal  = self::lc( $order->get_billing_postcode() );
		$country = self::lc( $order->get_billing_country() );
		if ( '' !== $city )    { $addr['city'] = $city; }
		if ( '' !== $region )  { $addr['region'] = $region; }
		if ( '' !== $postal )  { $addr['postal_code'] = $postal; }
		if ( '' !== $country ) { $addr['country'] = $country; }
		if ( ! empty( $addr ) ) {
			$ud['address'] = $addr;
		}
		return $ud;
	}

	
	public static function google_ads_user_identifiers( $order ) {
		$out = array();
		$em  = self::normalize_and_hash( 'em', (string) $order->get_billing_email() );
		$ph  = self::normalize_and_hash( 'ph', (string) $order->get_billing_phone() );
		if ( '' !== $em ) { $out[] = array( 'hashedEmail' => $em ); }
		if ( '' !== $ph ) { $out[] = array( 'hashedPhoneNumber' => $ph ); }

		$addr   = array();
		$fn     = self::normalize_and_hash( 'fn', (string) $order->get_billing_first_name() );
		$ln     = self::normalize_and_hash( 'ln', (string) $order->get_billing_last_name() );
		$street = self::normalize_and_hash( 'street', trim( $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() ) );
		if ( '' !== $fn )     { $addr['hashedFirstName'] = $fn; }
		if ( '' !== $ln )     { $addr['hashedLastName'] = $ln; }
		if ( '' !== $street ) { $addr['hashedStreetAddress'] = $street; }
		$city    = trim( (string) $order->get_billing_city() );
		$region  = trim( (string) $order->get_billing_state() );
		$postal  = trim( (string) $order->get_billing_postcode() );
		$country = trim( (string) $order->get_billing_country() );
		if ( '' !== $city )    { $addr['city'] = $city; }
		if ( '' !== $region )  { $addr['state'] = $region; }
		if ( '' !== $postal )  { $addr['postalCode'] = $postal; }
		if ( '' !== $country ) { $addr['countryCode'] = $country; }
		if ( ! empty( $addr ) ) {
			$out[] = array( 'addressInfo' => $addr );
		}
		return $out;
	}

	
	public static function build_user_data( $order, array $ids = array() ) {
		$ud = array();

		$hashed = array(
			'em'      => $order->get_billing_email(),
			'ph'      => $order->get_billing_phone(),
			'fn'      => $order->get_billing_first_name(),
			'ln'      => $order->get_billing_last_name(),
			'ct'      => $order->get_billing_city(),
			'st'      => $order->get_billing_state(),
			'zp'      => $order->get_billing_postcode(),
			'country' => $order->get_billing_country(),
		);

		
		$customer_id = (int) $order->get_customer_id();
		$external_id = $customer_id > 0
			? (string) $customer_id
			: ( ! empty( $ids['external_id'] ) ? (string) $ids['external_id'] : (string) $order->get_id() );
		$hashed['external_id'] = $external_id;

		foreach ( $hashed as $key => $raw ) {
			$h = self::normalize_and_hash( $key, (string) $raw );
			if ( '' !== $h ) {
				$ud[ $key ] = array( $h );
			}
		}

		
		$fbp = ! empty( $ids['fbp'] ) ? (string) $ids['fbp'] : '';
		$fbc = ! empty( $ids['fbc'] ) ? (string) $ids['fbc'] : '';
		if ( '' !== $fbp ) {
			$ud['fbp'] = $fbp;
		}
		if ( '' !== $fbc ) {
			$ud['fbc'] = $fbc;
		}

		$ip = (string) $order->get_customer_ip_address();
		$ua = (string) $order->get_customer_user_agent();
		if ( '' !== $ip ) {
			$ud['client_ip_address'] = $ip;
		}
		if ( '' !== $ua ) {
			$ud['client_user_agent'] = $ua;
		}

		return $ud;
	}
}
