<?php


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

class AIEO_Goal_Tracker {

	const CONFIG_OPT = 'aieo_goal_config';

	
	
	

	
	public static function config() {
		$raw = get_option( self::CONFIG_OPT, array() );
		$cfg = is_array( $raw ) ? $raw : array();
		return array(
			'forms'   => isset( $cfg['forms'] ) && is_array( $cfg['forms'] ) ? $cfg['forms'] : array(),
			'builtin' => array_merge(
				
				array( 'newsletter_signup' => 1, 'course_enroll' => 1, 'lesson_complete' => 1, 'user_register' => 0 ),
				isset( $cfg['builtin'] ) && is_array( $cfg['builtin'] ) ? array_map( 'intval', $cfg['builtin'] ) : array()
			),
		);
	}

	public static function save_config( array $forms, array $builtin ) {
		$cur   = self::config();
		$clean = array( 'forms' => array(), 'builtin' => array() );
		foreach ( $forms as $key => $f ) {
			$key = sanitize_key( (string) $key );
			if ( '' === $key ) {
				continue;
			}
			$prev = isset( $cur['forms'][ $key ] ) ? $cur['forms'][ $key ] : array();
			$clean['forms'][ $key ] = array(
				'on'      => empty( $f['on'] ) ? 0 : 1,
				'label'   => sanitize_text_field( (string) ( $f['label'] ?? ( $prev['label'] ?? '' ) ) ),
				
				'context' => sanitize_textarea_field( (string) ( $f['context'] ?? ( $prev['context'] ?? '' ) ) ),
			);
		}
		foreach ( array( 'newsletter_signup', 'course_enroll', 'lesson_complete', 'user_register' ) as $b ) {
			$clean['builtin'][ $b ] = empty( $builtin[ $b ] ) ? 0 : 1;
		}
		
		$clean['gravity_attribution'] = empty( $builtin['gravity_attribution'] ) ? 0 : 1;
		update_option( self::CONFIG_OPT, $clean, false );
	}

	
	public static function gravity_attribution_on() {
		$raw = get_option( self::CONFIG_OPT, array() );
		return is_array( $raw ) && ! empty( $raw['gravity_attribution'] );
	}

	
	private static function form_on( $provider, $form_id ) {
		$cfg = self::config();
		$key = sanitize_key( $provider . '_' . (int) $form_id );
		return ! empty( $cfg['forms'][ $key ]['on'] );
	}

	private static function builtin_on( $key ) {
		$cfg = self::config();
		return ! empty( $cfg['builtin'][ $key ] );
	}

	
	
	

	
	public static function available_forms() {
		$out = array();
		
		if ( class_exists( 'GFAPI' ) ) {
			foreach ( (array) GFAPI::get_forms( true ) as $f ) {
				if ( isset( $f['id'] ) ) {
					$out[ 'gravity_' . (int) $f['id'] ] = array( 'provider' => 'gravity', 'form_id' => (int) $f['id'], 'title' => (string) ( $f['title'] ?? ( '#' . $f['id'] ) ) );
				}
			}
		}
		return $out;
	}

	
	
	

	public static function register() {
		
		if ( class_exists( 'GFForms' ) ) {
			add_action( 'gform_after_submission', static function ( $entry, $form ) {
				$id = isset( $form['id'] ) ? (int) $form['id'] : 0;
				if ( self::form_on( 'gravity', $id ) ) {
					
					
					$attr = array_filter( array_intersect_key(
						self::attribution(),
						array( 'utm_source' => 1, 'utm_medium' => 1, 'utm_campaign' => 1 )
					) );
					self::record( 'form_submit_gravity', array_merge( array( 'form' => $id ), $attr ) );
				}
			}, 10, 2 );

			
			
			
			
			foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term' ) as $p ) {
				add_filter( "gform_field_value_{$p}", static function ( $value ) use ( $p ) {
					$a = self::attribution();
					return '' !== $a[ $p ] ? $a[ $p ] : $value;
				} );
			}
			
			
			add_filter( 'gform_field_value_handl_landing_page', static function ( $value ) {
				$a = self::attribution();
				return '' !== $a['landing_page'] ? $a['landing_page'] : $value;
			} );
			add_filter( 'gform_field_value_handl_original_ref', static function ( $value ) {
				$a = self::attribution();
				return '' !== $a['referrer'] ? $a['referrer'] : $value;
			} );

			
			
			
			add_filter( 'gform_notification', array( __CLASS__, 'append_attribution_to_notification' ), 10, 3 );

			
			
			
			
			
			
			
			
			
			
			if ( self::gravity_attribution_on() ) {
				add_action( 'wp_footer', array( __CLASS__, 'print_first_touch_js' ), 5 );
				
				
				
				
				add_filter( 'rocket_excluded_inline_js_content', static function ( $excluded ) {
					$excluded[] = 'aieo_attr';
					return $excluded;
				} );
				add_filter( 'rocket_delay_js_exclusions', static function ( $excluded ) {
					$excluded[] = 'aieo-first-touch';
					$excluded[] = 'aieo_attr';
					return $excluded;
				} );
			}

			
			
			
			
			add_filter( 'gform_hubspot_submission_data', static function ( $submission_data ) {
				if ( self::gravity_attribution_on()
					&& isset( $submission_data['context']['pageUri'] )
					&& is_string( $submission_data['context']['pageUri'] ) ) {
					$parts = explode( '?', $submission_data['context']['pageUri'] );
					$submission_data['context']['pageUri'] = $parts[0];
				}
				return $submission_data;
			} );
		}
		
		if ( defined( 'NEWSLETTER_VERSION' ) || class_exists( 'Newsletter' ) ) {
			add_action( 'newsletter_user_confirmed', static function () {
				if ( self::builtin_on( 'newsletter_signup' ) ) {
					self::record( 'newsletter_signup' );
				}
			} );
		}
		if ( defined( 'LEARNDASH_VERSION' ) ) {
			add_action( 'learndash_update_course_access', static function ( $user_id, $course_id, $access_list, $remove ) {
				if ( ! $remove && self::builtin_on( 'course_enroll' ) ) {
					self::record( 'course_enroll', array( 'course' => (int) $course_id ) );
				}
			}, 10, 4 );
			add_action( 'learndash_lesson_completed', static function ( $data ) {
				if ( self::builtin_on( 'lesson_complete' ) ) {
					$lesson = isset( $data['lesson'] ) && is_object( $data['lesson'] ) ? (int) $data['lesson']->ID : 0;
					self::record( 'lesson_complete', array( 'lesson' => $lesson ) );
				}
			} );
		}
		add_action( 'user_register', static function () {
			if ( self::builtin_on( 'user_register' ) ) {
				self::record( 'user_register' );
			}
		} );
		
		add_action( 'aieo_goal', array( __CLASS__, 'record' ), 10, 2 );
	}

	
	
	

	
	public static function print_first_touch_js() {
		?>
<script id="aieo-first-touch">(function(){try{
var r=document.referrer||'',h=location.hostname.replace(/^www\./,'');
if(r){var m=r.match(/^https?:\/\/([^\/]+)/);if(m&&m[1].replace(/^www\./,'')===h){r='';}}
var tagged=/[?&](utm_[a-z]+|gclid|fbclid|gad_source)=/.test(location.search);
var has=document.cookie.indexOf('aieo_attr=')!==-1;
/* LAST NON-DIRECT TOUCH: a new EXTERNAL arrival (referrer or tagged URL)
   starts a new attribution episode and overwrites; internal navigation and
   direct revisits never do. Without this, one old direct visit pins the
   cookie forever and later campaign/referral arrivals are lost. */
if(has&&!r&&!tagged)return;
if(!has||r||tagged){
var v=encodeURIComponent(JSON.stringify({r:r.slice(0,500),l:(location.href||'').slice(0,800)}));
document.cookie='aieo_attr='+v+'; max-age=7776000; path=/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');
}
}catch(e){}})();</script>
		<?php
	}

	
	private static function external_referrer( $url ) {
		$url = trim( (string) $url );
		if ( '' === $url ) {
			return '';
		}
		$host = preg_replace( '/^www\./', '', strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) ) );
		$home = preg_replace( '/^www\./', '', strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) );
		return ( '' === $host || $host === $home ) ? '' : $url;
	}

	
	public static function attribution() {
		static $memo = null;
		if ( null !== $memo ) {
			return $memo;
		}
		$out = array(
			'utm_source' => '', 'utm_medium' => '', 'utm_campaign' => '',
			'utm_content' => '', 'utm_term' => '', 'gclid' => '', 'fbclid' => '',
			'landing_page' => '', 'referrer' => '',
		);

		
		$session = '';
		foreach ( array( 'aieo_attr_sid', 'aieo_session_id' ) as $c ) {
			if ( isset( $_COOKIE[ $c ] ) && '' !== $_COOKIE[ $c ] ) {
				$session = sanitize_text_field( wp_unslash( $_COOKIE[ $c ] ) );
				break;
			}
		}
		if ( '' !== $session ) {
			global $wpdb;
			$st = $wpdb->prefix . 'aieo_sessions_statics';
			if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $st ) ) === $st ) {
				
				$row = $wpdb->get_row( $wpdb->prepare(
					"SELECT landing_page, referrer FROM `{$st}` WHERE session_id = %s LIMIT 1",
					$session
				), ARRAY_A );
				if ( $row ) {
					$out['landing_page'] = (string) $row['landing_page'];
					$out['referrer']     = (string) $row['referrer'];
					$query = (string) wp_parse_url( $out['landing_page'], PHP_URL_QUERY );
					if ( '' !== $query ) {
						parse_str( $query, $params );
						foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'fbclid' ) as $p ) {
							if ( isset( $params[ $p ] ) && is_string( $params[ $p ] ) ) {
								$out[ $p ] = sanitize_text_field( $params[ $p ] );
							}
						}
					}
				}
			}
		}

		
		
		
		
		if ( ( '' === $out['landing_page'] || '' === $out['referrer'] ) && isset( $_COOKIE['aieo_attr'] ) ) {
			$ft = json_decode( urldecode( (string) wp_unslash( $_COOKIE['aieo_attr'] ) ), true );
			if ( is_array( $ft ) ) {
				if ( '' === $out['landing_page'] && ! empty( $ft['l'] ) && is_string( $ft['l'] ) ) {
					$out['landing_page'] = esc_url_raw( $ft['l'] );
					$query = (string) wp_parse_url( $out['landing_page'], PHP_URL_QUERY );
					if ( '' !== $query ) {
						parse_str( $query, $params );
						foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'fbclid' ) as $p ) {
							if ( '' === $out[ $p ] && isset( $params[ $p ] ) && is_string( $params[ $p ] ) ) {
								$out[ $p ] = sanitize_text_field( $params[ $p ] );
							}
						}
					}
				}
				if ( '' === $out['referrer'] && ! empty( $ft['r'] ) && is_string( $ft['r'] ) ) {
					$out['referrer'] = esc_url_raw( $ft['r'] );
				}
			}
		}

		
		
		
		foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'fbclid' ) as $p ) {
			if ( '' === $out[ $p ] && isset( $_COOKIE[ $p ] ) && '' !== $_COOKIE[ $p ] ) {
				$out[ $p ] = sanitize_text_field( wp_unslash( $_COOKIE[ $p ] ) );
			}
		}
		if ( '' === $out['landing_page'] && isset( $_COOKIE['handl_landing_page'] ) ) {
			$out['landing_page'] = esc_url_raw( wp_unslash( $_COOKIE['handl_landing_page'] ) );
		}
		if ( '' === $out['referrer'] && isset( $_COOKIE['handl_original_ref'] ) ) {
			$out['referrer'] = esc_url_raw( wp_unslash( $_COOKIE['handl_original_ref'] ) );
		}

		
		foreach ( array( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'fbclid' ) as $p ) {
			
			if ( '' === $out[ $p ] && isset( $_GET[ $p ] ) && is_string( $_GET[ $p ] ) ) {
				
				$out[ $p ] = sanitize_text_field( wp_unslash( $_GET[ $p ] ) );
			}
		}

		
		
		
		$out['referrer'] = self::external_referrer( $out['referrer'] );

		$memo = $out;
		return $memo;
	}

	
	public static function append_attribution_to_notification( $notification, $form, $entry ) {
		if ( ! self::gravity_attribution_on() || ! class_exists( 'AIEO_Attribution_Block' ) ) {
			return $notification;
		}
		if ( ! self::notification_is_staff( $notification ) ) {
			return $notification;
		}
		
		
		
		
		
		
		$entry_a = self::entry_attribution( $form, $entry );
		$a       = self::attribution();
		foreach ( $entry_a as $k => $v ) {
			if ( 'landing_page' === $k || 'referrer' === $k ) {
				if ( '' === (string) $a[ $k ] ) {
					$a[ $k ] = $v; 
				}
			} else {
				$a[ $k ] = $v; 
			}
		}
		$a['referrer'] = self::external_referrer( $a['referrer'] );

		
		
		
		foreach ( $a as $k => $v ) {
			$a[ $k ] = str_replace( array( '{', '}' ), '', (string) $v );
		}

		
		
		
		
		
		$rows  = array( __( 'Channel', 'ai-eshop-optimizer' ) => AIEO_Attribution_Block::channel_label( self::channel_guess( $a ) ) );
		$rows += AIEO_Attribution_Block::utm_rows( $a );
		$rows['gclid'] = isset( $a['gclid'] ) && '' !== $a['gclid'] ? $a['gclid'] : '—';
		$rows[ __( 'Landing page', 'ai-eshop-optimizer' ) ]      = '' !== $a['landing_page'] ? $a['landing_page'] : '—';
		$rows[ __( 'Original referrer', 'ai-eshop-optimizer' ) ] = '' !== $a['referrer'] ? $a['referrer'] : '—';
		
		
		$ip = is_array( $entry ) && ! empty( $entry['ip'] ) ? sanitize_text_field( (string) $entry['ip'] ) : ( isset( $a['ip'] ) ? $a['ip'] : '' );
		$rows['IP'] = '' !== $ip ? $ip : '—';

		
		
		$title = __( 'Campaign attribution', 'ai-eshop-optimizer' );
		$fmt   = isset( $notification['message_format'] ) ? strtolower( (string) $notification['message_format'] ) : 'html';
		$block = ( 'text' === $fmt )
			? "\n\n" . AIEO_Attribution_Block::render_plain( $title, $rows )
			: AIEO_Attribution_Block::render_html( $title, $rows );

		$notification['message'] = (string) ( $notification['message'] ?? '' ) . $block;
		return $notification;
	}

	
	private static function notification_is_staff( $notification ) {
		$to_type = isset( $notification['toType'] ) ? (string) $notification['toType'] : 'email';
		if ( 'field' === $to_type || ! empty( $notification['toField'] ) ) {
			return false;
		}
		$candidates = array( isset( $notification['to'] ) ? (string) $notification['to'] : '' );
		if ( 'routing' === $to_type ) {
			if ( empty( $notification['routing'] ) || ! is_array( $notification['routing'] ) ) {
				return false; 
			}
			foreach ( $notification['routing'] as $rule ) {
				$candidates[] = isset( $rule['email'] ) ? (string) $rule['email'] : '';
			}
		}
		foreach ( $candidates as $c ) {
			$c = trim( $c );
			if ( '' !== $c && ctype_digit( $c ) ) {
				return false; 
			}
			
			
			if ( false !== strpos( str_replace( '{admin_email}', '', $c ), '{' ) ) {
				return false;
			}
		}
		return true;
	}

	
	private static function entry_attribution( $form, $entry ) {
		$map = array(
			'utm_source'         => 'utm_source',
			'utm_medium'         => 'utm_medium',
			'utm_campaign'       => 'utm_campaign',
			'utm_content'        => 'utm_content',
			'utm_term'           => 'utm_term',
			'gclid'              => 'gclid',
			'fbclid'             => 'fbclid',
			'handl_landing_page' => 'landing_page',
			'handl_original_ref' => 'referrer',
			'handl_ip'           => 'ip',
		);
		$out = array();
		if ( empty( $form['fields'] ) || ! is_array( $entry ) ) {
			return $out;
		}
		foreach ( $form['fields'] as $field ) {
			$param = is_object( $field ) ? (string) ( $field->inputName ?? '' ) : (string) ( $field['inputName'] ?? '' );
			if ( '' === $param || ! isset( $map[ $param ] ) ) {
				continue;
			}
			$id  = is_object( $field ) ? (string) $field->id : (string) ( $field['id'] ?? '' );
			$val = isset( $entry[ $id ] ) ? trim( (string) $entry[ $id ] ) : '';
			if ( '' !== $val ) {
				$out[ $map[ $param ] ] = sanitize_text_field( $val );
			}
		}
		return $out;
	}

	
	private static function channel_guess( array $a ) {
		$source = strtolower( $a['utm_source'] );
		$medium = strtolower( $a['utm_medium'] );
		if ( false !== strpos( $medium, 'cpc' ) || false !== strpos( $medium, 'paid' ) || false !== strpos( $medium, 'ppc' ) ) {
			if ( false !== strpos( $source, 'google' ) ) { return 'paid_google'; }
			if ( false !== strpos( $source, 'facebook' ) || false !== strpos( $source, 'instagram' ) || false !== strpos( $source, 'meta' ) || false !== strpos( $source, 'fb' ) ) { return 'paid_facebook'; }
			if ( false !== strpos( $source, 'tiktok' ) ) { return 'paid_tiktok'; }
			if ( false !== strpos( $source, 'bing' ) || false !== strpos( $source, 'microsoft' ) ) { return 'paid_microsoft'; }
			return '' !== $source ? 'paid_' . preg_replace( '/[^a-z0-9]+/', '', $source ) : 'paid';
		}
		
		if ( '' !== (string) ( isset( $a['gclid'] ) ? $a['gclid'] : '' ) ) {
			return 'paid_google';
		}
		if ( false !== strpos( $medium, 'email' ) || false !== strpos( $source, 'newsletter' ) || false !== strpos( $source, 'klaviyo' ) ) {
			return 'newsletter';
		}
		if ( false !== strpos( $medium, 'social' ) || in_array( $source, array( 'facebook', 'instagram', 'tiktok' ), true ) ) {
			return 'social';
		}
		
		
		if ( '' !== (string) ( isset( $a['fbclid'] ) ? $a['fbclid'] : '' ) ) {
			return 'social';
		}
		if ( '' !== $source || '' !== $medium || '' !== $a['utm_campaign'] ) {
			
			
			return trim( $source . ' / ' . $medium, ' /' );
		}
		$ref_host = strtolower( (string) wp_parse_url( $a['referrer'], PHP_URL_HOST ) );
		if ( '' !== $ref_host ) {
			foreach ( array( 'google.', 'bing.', 'yahoo.', 'duckduckgo.', 'ecosia.' ) as $se ) {
				if ( false !== strpos( $ref_host, $se ) ) {
					return 'organic_search';
				}
			}
			if ( false !== strpos( $ref_host, 'facebook.' ) || false !== strpos( $ref_host, 'instagram.' ) || false !== strpos( $ref_host, 'tiktok.' ) || 't.co' === $ref_host ) {
				return 'social';
			}
			$site_host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
			if ( $ref_host !== $site_host ) {
				return 'referral';
			}
		}
		return 'direct';
	}

	
	public static function record( $goal_key, $meta = array() ) {
		global $wpdb;
		$goal_key = sanitize_key( str_replace( ':', '_', (string) $goal_key ) );
		if ( '' === $goal_key ) {
			return;
		}

		
		
		$session = '';
		foreach ( array( 'aieo_attr_sid', 'aieo_session_id' ) as $c ) {
			if ( isset( $_COOKIE[ $c ] ) && '' !== $_COOKIE[ $c ] ) {
				$session = sanitize_text_field( wp_unslash( $_COOKIE[ $c ] ) );
				break;
			}
		}
		if ( '' === $session ) {
			return;
		}

		$t = $wpdb->prefix . 'aieo_sessions';
		if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) {
			return;
		}

		
		
		$page = (string) wp_get_referer();
		if ( '' === $page && isset( $_SERVER['HTTP_REFERER'] ) ) {
			$page = esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
		}
		$page = substr( $page, 0, 2048 );

		$now_ms = (int) round( microtime( true ) * 1000 );
		$data   = wp_json_encode( array_merge(
			array( 'goal_key' => $goal_key ),
			is_array( $meta ) ? array_map( 'strval', array_filter( $meta, 'is_scalar' ) ) : array()
		) );

		
		
		
		$wpdb->query( $wpdb->prepare(
			"INSERT IGNORE INTO `{$t}`
				(event_id, session_id, user_id, event_type, event_data, page_url, timestamp)
			 VALUES (%s, %s, %d, 'goal', %s, %s, %d)",
			substr( md5( $session . '|' . $now_ms . '|goal|' . $goal_key . '|' . wp_rand() ), 0, 36 ),
			$session,
			get_current_user_id(),
			$data,
			$page,
			$now_ms
		) );

		
		do_action( 'aieo_goal_recorded', $goal_key, is_array( $meta ) ? $meta : array(), array(
			'session_id'  => $session,
			'page_url'    => $page,
			'attribution' => self::attribution(),
			'ip'          => isset( $_SERVER['REMOTE_ADDR'] ) ? (string) $_SERVER['REMOTE_ADDR'] : '',
			'ua'          => isset( $_SERVER['HTTP_USER_AGENT'] ) ? (string) $_SERVER['HTTP_USER_AGENT'] : '',
		) );
	}

	
	
	

	
	public static function form_substance( $provider, $form_id ) {
		$form_id = (int) $form_id;
		$lines   = array();
		if ( 'gravity' === $provider && class_exists( 'GFAPI' ) ) {
			$f = GFAPI::get_form( $form_id );
			if ( is_array( $f ) ) {
				$lines[] = 'FORM TITLE: ' . (string) ( $f['title'] ?? '' );
				if ( ! empty( $f['description'] ) ) {
					$lines[] = 'FORM DESCRIPTION: ' . (string) $f['description'];
				}
				foreach ( (array) ( $f['fields'] ?? array() ) as $fl ) {
					$label = is_object( $fl ) ? (string) ( $fl->label ?? '' ) : (string) ( $fl['label'] ?? '' );
					$type  = is_object( $fl ) ? (string) ( $fl->type ?? '' ) : (string) ( $fl['type'] ?? '' );
					if ( '' !== $label ) {
						$lines[] = "FIELD: {$label} ({$type})";
					}
				}
				if ( ! empty( $f['button']['text'] ) ) {
					$lines[] = 'SUBMIT BUTTON: ' . (string) $f['button']['text'];
				}
			}
		}
		return implode( "\n", array_filter( $lines ) );
	}

	
	public static function analyze_form( $provider, $form_id ) {
		if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) ) {
			require_once AIEO_DIR . 'includes/seo/class-aieo-seo-ai-assist.php';
		}
		if ( ! AIEO_SEO_AI_Assist::available() ) {
			return new WP_Error( 'aieo_ai_unavailable', __( 'AI is not configured — paste the Rooster Content Connector token first.', 'ai-eshop-optimizer' ) );
		}
		$substance = self::form_substance( $provider, $form_id );
		if ( '' === $substance ) {
			return new WP_Error( 'aieo_form_empty', __( 'Could not read that form’s fields.', 'ai-eshop-optimizer' ) );
		}
		$api  = AI_eShop_Chat_Anthropic_API::get_instance();
		$resp = $api->chat_completion(
			array( array(
				'role'    => 'user',
				'content' => "Analyse this web form and identify the CONVERSION GOAL it captures.\n\n{$substance}\n\n"
					. 'Return ONLY minified JSON: {"label": "<2-4 word goal name>", "context": "<1-2 sentences: what the visitor is trying to achieve by submitting, and what business outcome a submission represents. Same language as the form.>"}',
			) ),
			array(
				'system'      => 'You analyse web forms to identify conversion goals for SEO copywriting. Be concrete about visitor intent. Return only the JSON object.',
				'max_tokens'  => 300,
				'temperature' => 0.3,
			)
		);
		if ( is_wp_error( $resp ) ) {
			return $resp;
		}
		$text = '';
		if ( isset( $resp['content'] ) && is_array( $resp['content'] ) ) {
			foreach ( $resp['content'] as $blk ) {
				if ( is_array( $blk ) && 'text' === ( $blk['type'] ?? '' ) && isset( $blk['text'] ) ) {
					$text .= (string) $blk['text'];
				}
			}
		}
		$json = json_decode( trim( preg_replace( '/^```(?:json)?|```$/m', '', $text ) ), true );
		if ( ! is_array( $json ) || empty( $json['context'] ) ) {
			return new WP_Error( 'aieo_ai_parse', __( 'The AI returned an unexpected format.', 'ai-eshop-optimizer' ) );
		}
		$label   = sanitize_text_field( (string) ( $json['label'] ?? '' ) );
		$context = sanitize_textarea_field( (string) $json['context'] );

		
		$cfg = self::config();
		$key = sanitize_key( $provider . '_' . (int) $form_id );
		if ( ! isset( $cfg['forms'][ $key ] ) ) {
			$cfg['forms'][ $key ] = array( 'on' => 0, 'label' => '', 'context' => '' );
		}
		$cfg['forms'][ $key ]['label']   = $label;
		$cfg['forms'][ $key ]['context'] = $context;
		update_option( self::CONFIG_OPT, $cfg, false );

		return array( 'label' => $label, 'context' => $context );
	}

	
	public static function goal_context_for( $post ) {
		if ( ! $post instanceof WP_Post ) {
			return array();
		}
		$cfg     = self::config();
		$content = (string) $post->post_content;
		$out     = array();
		foreach ( $cfg['forms'] as $key => $f ) {
			if ( empty( $f['on'] ) || '' === (string) ( $f['context'] ?? '' ) ) {
				continue;
			}
			if ( ! preg_match( '/^gravity_(\d+)$/', (string) $key, $m ) ) {
				continue;
			}
			$id = (int) $m[1];
			
			$present = (bool) preg_match( '/\[gravityforms?\s[^\]]*id=["\']?' . $id . '["\']?/', $content )
				|| false !== strpos( $content, '"formId":"' . $id . '"' ) || false !== strpos( $content, '"formId":' . $id );
			if ( $present ) {
				$label = '' !== (string) ( $f['label'] ?? '' ) ? $f['label'] : $key;
				$out[] = $label . ': ' . $f['context'];
			}
		}
		return $out;
	}
}
