<?php
/**
 * Create AI Jobs Table Migration.
 *
 * @package Ugcify
 */

namespace Ugcify\Database\Migrations;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * CreateAIJobsTable migration class.
 */
class CreateAIJobsTable {
	/**
	 * WordPress database instance.
	 *
	 * @var \wpdb
	 */
	private $wpdb;

	/**
	 * Constructor.
	 *
	 * @param \wpdb $wpdb WordPress database instance.
	 */
	public function __construct( $wpdb ) {
		$this->wpdb = $wpdb;
	}

	/**
	 * Run migration.
	 *
	 * @return string SQL statement.
	 */
	public function run() {
		$table_name      = $this->wpdb->prefix . 'ugcify_ai_jobs';
		$charset_collate = $this->wpdb->get_charset_collate();

		return "CREATE TABLE $table_name (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			review_id BIGINT(20) UNSIGNED NOT NULL,
			media_id BIGINT(20) UNSIGNED NOT NULL,
			media_index INT(10) UNSIGNED NOT NULL DEFAULT 0,
			attachment_id BIGINT(20) UNSIGNED DEFAULT NULL,
			image_url_hash CHAR(64) NOT NULL,
			status VARCHAR(32) NOT NULL DEFAULT 'queued',
			attempts TINYINT(3) UNSIGNED NOT NULL DEFAULT 0,
			locked_until DATETIME DEFAULT NULL,
			provider VARCHAR(32) NOT NULL DEFAULT 'anthropic',
			model VARCHAR(64) NOT NULL DEFAULT '',
			candidate_product_ids LONGTEXT DEFAULT NULL,
			suggestions LONGTEXT DEFAULT NULL,
			error_code VARCHAR(64) DEFAULT NULL,
			created_at DATETIME NOT NULL,
			updated_at DATETIME NOT NULL,
			PRIMARY KEY (id),
			UNIQUE KEY uniq_media_hash (media_id, image_url_hash),
			KEY review_media (review_id, media_index),
			KEY media_id (media_id),
			KEY status (status)
		) $charset_collate;";
	}
}
