<?php

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

if ( ! class_exists( 'AIEO_SEO_Store' ) ) {
	require_once AIEO_DIR . 'includes/seo/class-aieo-seo-store.php';
}
if ( ! class_exists( 'AIEO_DFS_Rankings' ) ) {
	require_once AIEO_DIR . 'includes/seo/class-aieo-dfs-rankings.php';
}
if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) ) {
	require_once AIEO_DIR . 'includes/seo/class-aieo-seo-ai-assist.php';
}

class AIEO_SEO_Write_Queue {

	
	public static function daily_cap() {
		$n = class_exists( 'AIEO_DMM_Settings' ) ? AIEO_DMM_Settings::get( 'seo.ai_write_daily', 5 ) : 5;
		return max( 0, min( 50, (int) $n ) );
	}

	
	public static function autopilot() {
		return class_exists( 'AIEO_DMM_Settings' ) && (bool) AIEO_DMM_Settings::get( 'seo.autopilot', false );
	}

	public static function available() {
		return class_exists( 'AIEO_SEO_AI_Assist' ) && AIEO_SEO_AI_Assist::available()
			&& class_exists( 'AIEO_DataForSEO' ) && AIEO_DataForSEO::is_connected();
	}

	
	public static function run( $limit = null ) {
		$out = array( 'drafted' => 0, 'applied' => 0, 'skipped' => 0 );
		if ( ! self::available() ) {
			$out['skipped'] = 'ai_or_dfs_unavailable';
			return $out;
		}
		$limit = null === $limit ? self::daily_cap() : max( 0, (int) $limit );
		if ( 0 === $limit ) {
			$out['skipped'] = 'cap_zero';
			return $out;
		}
		foreach ( self::candidates( $limit ) as $c ) {
			$r = self::process( (string) $c['object_type'], (int) $c['object_id'] );
			if ( 'applied' === $r ) {
				$out['applied']++;
			} elseif ( 'drafted' === $r ) {
				$out['drafted']++;
			} else {
				$out['skipped'] = is_int( $out['skipped'] ) ? $out['skipped'] + 1 : 1;
			}
		}
		return $out;
	}

	
	public static function candidates( $limit ) {
		global $wpdb;
		$sp = $wpdb->prefix . 'aieo_seo_posts';
		$sc = $wpdb->prefix . 'aieo_seo_scores';
		
		$rows = $wpdb->get_results( $wpdb->prepare(
			"SELECT sp.object_type, sp.object_id
			   FROM `{$sp}` sp
			   LEFT JOIN `{$sc}` sc ON sc.object_type = sp.object_type AND sc.object_id = sp.object_id
			  WHERE sp.search_volume_total > 0
			    AND COALESCE(sp.ai_locked, 0) = 0
			    AND COALESCE(sp.ai_state, 'none') <> 'suggested'
			    AND ( sp.striking_count > 0 OR COALESCE(sc.score, 0) < 70 )
			  ORDER BY (sp.striking_count > 0) DESC, sp.search_volume_total DESC
			  LIMIT %d",
			(int) $limit
		), ARRAY_A );
		$out = array();
		foreach ( (array) $rows as $r ) {
			if ( post_type_exists( (string) $r['object_type'] ) ) { 
				$out[] = array( 'object_type' => (string) $r['object_type'], 'object_id' => (int) $r['object_id'] );
			}
		}
		return $out;
	}

	
	public static function process( $object_type, $object_id ) {
		$post = get_post( $object_id );
		if ( ! $post instanceof WP_Post || 'publish' !== $post->post_status ) {
			return 'skipped:no_post';
		}

		
		
		$row     = (array) AIEO_SEO_Store::get( $object_type, $object_id );
		$keyword = trim( (string) ( $row['focus_keyphrase'] ?? '' ) );
		if ( '' === $keyword ) {
			$ranked  = AIEO_DFS_Rankings::keywords_for( $object_type, $object_id, 1 );
			$keyword = isset( $ranked[0]['keyword'] ) ? (string) $ranked[0]['keyword'] : '';
		}
		if ( '' === $keyword ) {
			return 'skipped:no_keyword';
		}

		
		$serp = AIEO_DFS_Rankings::serp_context( $keyword );
		if ( is_wp_error( $serp ) ) {
			return 'skipped:' . $serp->get_error_code();
		}
		$extra   = array( 'TARGET SEARCH QUERY: ' . $keyword, 'LIVE GOOGLE SERP TO BEAT (top organic results):' );
		foreach ( array_slice( $serp['competitors'], 0, 8 ) as $i => $c ) {
			$extra[] = ( $i + 1 ) . '. ' . $c['title'] . ' — ' . mb_substr( (string) $c['description'], 0, 140 );
		}
		if ( ! empty( $serp['paa'] ) ) {
			$extra[] = 'PEOPLE ALSO ASK: ' . implode( ' | ', $serp['paa'] );
		}
		$secondary = AIEO_DFS_Rankings::keywords_for( $object_type, $object_id, 6 );
		if ( $secondary ) {
			$extra[] = 'SECONDARY KEYWORDS THIS PAGE ALREADY RANKS FOR: '
				. implode( ', ', array_map( static function ( $k ) { return $k['keyword'] . ' (pos ' . $k['position'] . ', vol ' . $k['volume'] . ')'; }, $secondary ) );
		}

		
		
		
		if ( class_exists( 'AIEO_Goal_Tracker' ) || file_exists( AIEO_DIR . 'includes/class-aieo-goal-tracker.php' ) ) {
			if ( ! class_exists( 'AIEO_Goal_Tracker' ) ) {
				require_once AIEO_DIR . 'includes/class-aieo-goal-tracker.php';
			}
			$goals = AIEO_Goal_Tracker::goal_context_for( $post );
			if ( $goals ) {
				$extra[] = 'THIS PAGE\'S CONVERSION GOAL(S) — write the description with a call to action toward these: '
					. implode( ' | ', $goals );
			}
		}

		$res = AIEO_SEO_AI_Assist::suggest( $post, $extra );
		if ( is_wp_error( $res ) ) {
			return 'skipped:' . $res->get_error_code();
		}
		if ( '' === (string) $res['title'] && '' === (string) $res['description'] ) {
			return 'skipped:empty_ai_result';
		}

		if ( self::autopilot() ) {
			return self::apply_fields( $object_type, $object_id, $res ) ? 'applied' : 'skipped:apply_failed';
		}

		AIEO_SEO_Store::save( $object_type, $object_id, array(
			'suggested_title'       => (string) $res['title'],
			'suggested_description' => (string) $res['description'],
			'suggested_keyphrase'   => (string) $res['keyphrase'],
			'suggested_at'          => current_time( 'mysql', true ),
			'ai_state'              => 'suggested',
		) );
		return 'drafted';
	}

	
	
	

	
	public static function approve( $object_type, $object_id ) {
		$row = (array) AIEO_SEO_Store::get( $object_type, $object_id );
		if ( 'suggested' !== (string) ( $row['ai_state'] ?? '' ) ) {
			return false;
		}
		$fields = array(
			'title'       => (string) ( $row['suggested_title'] ?? '' ),
			'description' => (string) ( $row['suggested_description'] ?? '' ),
			'keyphrase'   => (string) ( $row['suggested_keyphrase'] ?? '' ),
		);
		return self::apply_fields( $object_type, $object_id, $fields );
	}

	
	public static function dismiss( $object_type, $object_id ) {
		return (bool) AIEO_SEO_Store::save( $object_type, $object_id, array(
			'suggested_title'       => null,
			'suggested_description' => null,
			'suggested_keyphrase'   => null,
			'suggested_at'          => null,
			'ai_state'              => 'none',
		) );
	}

	
	private static function apply_fields( $object_type, $object_id, array $fields ) {
		$save = array(
			'suggested_title'       => null,
			'suggested_description' => null,
			'suggested_keyphrase'   => null,
			'suggested_at'          => null,
			'ai_state'              => 'applied',
		);
		if ( '' !== (string) ( $fields['title'] ?? '' ) ) {
			$save['title'] = (string) $fields['title'];
		}
		if ( '' !== (string) ( $fields['description'] ?? '' ) ) {
			$save['description'] = (string) $fields['description'];
		}
		if ( '' !== (string) ( $fields['keyphrase'] ?? '' ) ) {
			$save['focus_keyphrase']        = (string) $fields['keyphrase'];
			$save['focus_keyphrase_source'] = 'ai_write_queue';
		}
		$ok = AIEO_SEO_Store::save( $object_type, $object_id, $save );
		if ( $ok && class_exists( 'AIEO_SEO_Head_Builder' ) && method_exists( 'AIEO_SEO_Head_Builder', 'build' ) ) {
			AIEO_SEO_Head_Builder::build( $object_type, $object_id );
		}
		return (bool) $ok;
	}

	
	public static function pending( $limit = 50 ) {
		global $wpdb;
		$sp = $wpdb->prefix . 'aieo_seo_posts';
		
		$rows = $wpdb->get_results( $wpdb->prepare(
			"SELECT object_type, object_id, title, description, suggested_title, suggested_description, suggested_keyphrase, suggested_at
			   FROM `{$sp}`
			  WHERE ai_state = 'suggested'
			  ORDER BY suggested_at DESC
			  LIMIT %d",
			(int) $limit
		), ARRAY_A );
		return is_array( $rows ) ? $rows : array();
	}
}
