<?php

/**
 * Fired during plugin activation
 *
 * @link       https://insya.com/ai-magic
 * @since      1.0.0
 *
 * @package    Ai_Magic
 * @subpackage Ai_Magic/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Ai_Magic
 * @subpackage Ai_Magic/includes
 * @author     AI Magic <info@insya.com>
 */
class Ai_Magic_Activator {

	/**
	 * Plugin activation handler
	 *
	 * Creates database tables, migrates settings, and sets up default data.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {
		// Migrate legacy settings to new provider-based structure
		self::migrate_legacy_settings();

		// Insert page templates
		self::insert_page_templates();
	}

	/**
	 * Migrate legacy settings to new provider-based structure
	 *
	 * @since    1.0.8
	 */
	private static function migrate_legacy_settings() {
		// Check if migration already done
		if (get_option('ai_magic_settings_migrated', false)) {
			return;
		}

		// Migrate OpenAI legacy settings
		$legacy_api_key = get_option('ai_magic_api_key', '');
		if (!empty($legacy_api_key) && empty(get_option('ai_magic_api_key_openai', ''))) {
			update_option('ai_magic_api_key_openai', $legacy_api_key);
			update_option('ai_magic_openai_active', 'yes');
		}

		// Migrate model setting
		$legacy_model = get_option('ai_magic_model', '');
		if (!empty($legacy_model)) {
			// Determine which provider this model belongs to
			if (strpos($legacy_model, 'gpt') !== false) {
				update_option('ai_magic_openai_model', $legacy_model);
			} elseif (strpos($legacy_model, 'claude') !== false) {
				update_option('ai_magic_anthropic_model', $legacy_model);
			} elseif (strpos($legacy_model, 'gemini') !== false) {
				update_option('ai_magic_google_model', $legacy_model);
			}
		}

		// Migrate common settings to OpenAI provider (primary provider)
		$common_settings = array(
			'max_tokens' => get_option('ai_magic_max_tokens', ''),
			'temperature' => get_option('ai_magic_temperature', ''),
			'top_p' => get_option('ai_magic_top_p', ''),
			'frequency_penalty' => get_option('ai_magic_frequency_penalty', ''),
			'presence_penalty' => get_option('ai_magic_presence_penalty', '')
		);

		foreach ($common_settings as $setting => $value) {
			if (!empty($value) && empty(get_option("ai_magic_openai_{$setting}", ''))) {
				update_option("ai_magic_openai_{$setting}", $value);
			}
		}

		// Set default values for new providers if not set
		$provider_defaults = array(
			'anthropic' => array(
				'model' => 'claude-3-5-sonnet-20241022',
				'max_tokens' => 4096,
				'temperature' => 0.5
			),
			'google' => array(
				'model' => 'gemini-pro',
				'max_tokens' => 2048,
				'temperature' => 0.9
			)
		);

		foreach ($provider_defaults as $provider => $defaults) {
			foreach ($defaults as $setting => $value) {
				if (empty(get_option("ai_magic_{$provider}_{$setting}", ''))) {
					update_option("ai_magic_{$provider}_{$setting}", $value);
				}
			}
		}

		// Mark migration as complete
		update_option('ai_magic_settings_migrated', true);
	}

	/**
	 * Insert page templates
	 *
	 * @since    1.0.8
	 */
	private static function insert_page_templates() {
		// Check if templates class exists and call insert method
		if (class_exists('Ai_Magic_Templates')) {
			Ai_Magic_Templates::insert_page_templates();
		} else {
			// Load templates class if not already loaded
			$templates_file = plugin_dir_path(dirname(__FILE__)) . 'admin/class-ai-magic-templates.php';
			if (file_exists($templates_file)) {
				require_once $templates_file;
				if (class_exists('Ai_Magic_Templates')) {
					Ai_Magic_Templates::insert_page_templates();
				}
			}
		}
	}

}
