<?php

namespace Limb_Chatbot\Includes\Migrations;

use Limb_Chatbot\Includes\Services\Ai_Model_Catalog_Refresher;

/**
 * Migration for version 2.0.1.
 *
 * Refreshes AI model catalog: removes deprecated models and fixes orphaned
 * model references after deletion.
 *
 * @since 2.0.1
 */
class Migration_2_0_1 extends Abstract_Migration {

	/**
	 * Models to delete during this migration.
	 *
	 * @since 2.0.1
	 */
	private const DEPRECATED_MODELS = [
		'dall-e-2',
		'dall-e-3',
		'gpt-5.4-pro',
		'gpt-4-turbo',
		'gpt-5.5-mini',
		'gpt-5.5-nano',
		'grok-4-1-fast-reasoning',
		'grok-4-1-fast-non-reasoning',
		'grok-4-fast-reasoning',
		'grok-4-fast-non-reasoning',
	];

	/**
	 * @inheritDoc
	 */
	public function get_version(): string {
		return '2.0.1';
	}

	/**
	 * @inheritDoc
	 */
	public function get_description(): string {
		return 'Refresh AI model catalog: remove deprecated models and repair orphaned settings.';
	}

	/**
	 * @inheritDoc
	 */
	public function up(): bool {
		$refresher = new Ai_Model_Catalog_Refresher(
			fn( string $message, string $level = 'info' ) => $this->log( $message, $level ),
			fn( string $key, $value ) => $this->maybe_add_setting( $key, $value )
		);

		if ( ! $refresher->run( self::DEPRECATED_MODELS ) ) {
			return false;
		}

		$this->log( 'Migration 2.0.1 completed successfully.' );

		return true;
	}
}
