<?php

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

class AIEO_DMM_Conversions extends AIEO_DMM_Module_Base {

	const OPTION_KEY = 'aieo_dmm_conversions';

	public function get_id()       { return 'conversions'; }
	public function get_label()    { return __( 'Pixels & Conversions', 'ai-eshop-optimizer' ); }
	public function get_priority() { return 20; }
	public function get_group()    { return 'attribution'; }

	
	public function get_settings() {
		return self::get_config();
	}

	
	public static function get_config() {
		$stored = get_option( self::OPTION_KEY, array() );
		if ( ! is_array( $stored ) ) {
			$stored = array();
		}

		$legacy_ga4 = (string) get_option( 'aieo_google_ga4', '' );
		
		
		$ga_source_default = ( '' !== $legacy_ga4 ) ? 'aieo' : 'off';

		return array_merge( array(
			
			'fb_mode'            => 'off',   
			'fb_pixel_id'        => '',
			'fb_access_token'    => '',
			'fb_test_event_code' => '',
			
			'ga_source'          => $ga_source_default, 
			'ga_measurement_id'  => $legacy_ga4,
			'ga_api_secret'      => '',
			'ga_send_browser'    => 'yes',
			'ga_send_server'     => 'no',

			
			'google_tag_mode'      => 'off',  
			'gtm_container_id'     => '',      
			'ads_conversion_id'    => '',      
			'ads_conversion_label' => '',      

			
			'ads_server_ec'            => 'no',
			'ads_developer_token'      => '',
			'ads_customer_id'          => '',  
			'ads_login_customer_id'    => '',  
			'ads_oauth_client_id'      => '',
			'ads_oauth_client_secret'  => '',
			'ads_oauth_refresh_token'  => '',
			'ads_conversion_action_id' => '',  

			
			'tiktok_mode'            => 'off',   
			'tiktok_pixel_id'        => '',
			'tiktok_access_token'    => '',
			'tiktok_test_event_code' => '',

			
			
			'server_event_blocking'  => 'no',
		), $stored );
	}

	public function register_hooks() {
		add_action( 'wp_ajax_aieo_dmm_conversions_save', array( $this, 'ajax_save' ) );
	}

	public function render_subtab() {
		$this->render_template( 'conversions.php', array(
			'settings'       => $this->get_settings(),
			'sitekit_active' => $this->is_sitekit_active(),
			'recent_events'  => $this->get_recent_events( 15 ),
		) );
	}

	
	public function get_recent_events( $limit = 15 ) {
		global $wpdb;
		$t = $wpdb->prefix . 'aieo_server_events_log';
		if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) {
			return array();
		}
		
		return (array) $wpdb->get_results( $wpdb->prepare(
			"SELECT event_id, platform, event_name, order_id, status, http_code, fbtrace_id, value, currency, test_mode, created_at
			 FROM `{$t}` ORDER BY id DESC LIMIT %d",
			max( 1, (int) $limit )
		), ARRAY_A );
	}

	
	private function is_sitekit_active() {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			include_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		return function_exists( 'is_plugin_active' ) && is_plugin_active( 'google-site-kit/google-site-kit.php' );
	}

	public function ajax_save() {
		AIEO_DMM::verify_ajax_request();

		$current    = $this->get_settings();
		$fb_modes   = array( 'off', 'browser', 'capi', 'both' );
		$ga_sources = array( 'off', 'aieo', 'sitekit' );

		$fb_mode = isset( $_POST['fb_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['fb_mode'] ) ) : 'off';
		if ( ! in_array( $fb_mode, $fb_modes, true ) ) {
			$fb_mode = 'off';
		}

		$ga_source = isset( $_POST['ga_source'] ) ? sanitize_text_field( wp_unslash( $_POST['ga_source'] ) ) : 'off';
		if ( ! in_array( $ga_source, $ga_sources, true ) ) {
			$ga_source = 'off';
		}

		
		
		$fb_token = isset( $_POST['fb_access_token'] ) ? sanitize_text_field( wp_unslash( $_POST['fb_access_token'] ) ) : '';
		if ( '' === $fb_token ) {
			$fb_token = (string) $current['fb_access_token'];
		}
		$ga_secret = isset( $_POST['ga_api_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['ga_api_secret'] ) ) : '';
		if ( '' === $ga_secret ) {
			$ga_secret = (string) $current['ga_api_secret'];
		}

		$ga_measurement_id = isset( $_POST['ga_measurement_id'] ) ? sanitize_text_field( wp_unslash( $_POST['ga_measurement_id'] ) ) : '';

		
		$tag_modes = array( 'off', 'gtag', 'gtm' );
		$tag_mode  = isset( $_POST['google_tag_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['google_tag_mode'] ) ) : 'off';
		if ( ! in_array( $tag_mode, $tag_modes, true ) ) {
			$tag_mode = 'off';
		}

		
		$keep = function ( $key ) use ( $current ) {
			$v = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : ''; 
			return '' === $v ? (string) ( isset( $current[ $key ] ) ? $current[ $key ] : '' ) : $v;
		};

		
		$tt_modes = array( 'off', 'browser', 'server', 'both' );
		$tt_mode  = isset( $_POST['tiktok_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['tiktok_mode'] ) ) : 'off';
		if ( ! in_array( $tt_mode, $tt_modes, true ) ) {
			$tt_mode = 'off';
		}
		$tt_token = isset( $_POST['tiktok_access_token'] ) ? sanitize_text_field( wp_unslash( $_POST['tiktok_access_token'] ) ) : '';
		if ( '' === $tt_token ) {
			$tt_token = (string) ( isset( $current['tiktok_access_token'] ) ? $current['tiktok_access_token'] : '' );
		}

		$patch = array(
			'fb_mode'            => $fb_mode,
			'fb_pixel_id'        => isset( $_POST['fb_pixel_id'] ) ? preg_replace( '/[^0-9]/', '', (string) wp_unslash( $_POST['fb_pixel_id'] ) ) : '',
			'fb_access_token'    => $fb_token,
			'fb_test_event_code' => isset( $_POST['fb_test_event_code'] ) ? sanitize_text_field( wp_unslash( $_POST['fb_test_event_code'] ) ) : '',
			'ga_source'          => $ga_source,
			'ga_measurement_id'  => $ga_measurement_id,
			'ga_api_secret'      => $ga_secret,
			'ga_send_browser'    => ! empty( $_POST['ga_send_browser'] ) ? 'yes' : 'no',
			'ga_send_server'     => ! empty( $_POST['ga_send_server'] ) ? 'yes' : 'no',

			
			'google_tag_mode'      => $tag_mode,
			'gtm_container_id'     => isset( $_POST['gtm_container_id'] ) ? sanitize_text_field( wp_unslash( $_POST['gtm_container_id'] ) ) : '',
			'ads_conversion_id'    => isset( $_POST['ads_conversion_id'] ) ? sanitize_text_field( wp_unslash( $_POST['ads_conversion_id'] ) ) : '',
			'ads_conversion_label' => isset( $_POST['ads_conversion_label'] ) ? sanitize_text_field( wp_unslash( $_POST['ads_conversion_label'] ) ) : '',

			
			'ads_server_ec'            => ! empty( $_POST['ads_server_ec'] ) ? 'yes' : 'no',
			'ads_developer_token'      => $keep( 'ads_developer_token' ),
			'ads_customer_id'          => isset( $_POST['ads_customer_id'] ) ? preg_replace( '/[^0-9]/', '', (string) wp_unslash( $_POST['ads_customer_id'] ) ) : '',
			'ads_login_customer_id'    => isset( $_POST['ads_login_customer_id'] ) ? preg_replace( '/[^0-9]/', '', (string) wp_unslash( $_POST['ads_login_customer_id'] ) ) : '',
			'ads_oauth_client_id'      => isset( $_POST['ads_oauth_client_id'] ) ? sanitize_text_field( wp_unslash( $_POST['ads_oauth_client_id'] ) ) : '',
			'ads_oauth_client_secret'  => $keep( 'ads_oauth_client_secret' ),
			'ads_oauth_refresh_token'  => $keep( 'ads_oauth_refresh_token' ),
			'ads_conversion_action_id' => isset( $_POST['ads_conversion_action_id'] ) ? preg_replace( '/[^0-9]/', '', (string) wp_unslash( $_POST['ads_conversion_action_id'] ) ) : '',

			
			'tiktok_mode'            => $tt_mode,
			'tiktok_pixel_id'        => isset( $_POST['tiktok_pixel_id'] ) ? sanitize_text_field( wp_unslash( $_POST['tiktok_pixel_id'] ) ) : '',
			'tiktok_access_token'    => $tt_token,
			'tiktok_test_event_code' => isset( $_POST['tiktok_test_event_code'] ) ? sanitize_text_field( wp_unslash( $_POST['tiktok_test_event_code'] ) ) : '',

			
			
			'server_event_blocking'  => empty( $_POST['server_event_async'] ) ? 'yes' : 'no',
		);

		update_option( self::OPTION_KEY, $patch, false );

		
		
		if ( 'aieo' === $ga_source ) {
			update_option( 'aieo_google_ga4', $ga_measurement_id );
		}

		
		
		

		wp_send_json_success( array( 'message' => __( 'Conversion settings saved.', 'ai-eshop-optimizer' ) ) );
	}
}
