<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Fired during plugin activation.
 */
class Podcastmaker_Activator {
	const SCHEMA_VERSION = '4';

	/**
	 * Create the usage logs table and initialize default prompt sets.
	 */
	public static function activate() {
		self::create_tables();
		self::reset_prompt_data_to_bundled_defaults();
		update_option( 'podcastmaker_schema_version', self::SCHEMA_VERSION );
	}

	/**
	 * Upgrade database schema for existing installs.
	 */
	public static function maybe_upgrade() {
		$current_version = get_option( 'podcastmaker_schema_version', '0' );
		if ( version_compare( (string) $current_version, self::SCHEMA_VERSION, '>=' ) ) {
			return;
		}

		self::create_tables();
		self::reset_prompt_data_to_bundled_defaults();
		update_option( 'podcastmaker_schema_version', self::SCHEMA_VERSION );
	}

	/**
	 * Reset prompt data so only bundled prompt definitions remain.
	 */
	private static function reset_prompt_data_to_bundled_defaults() {
		if ( ! class_exists( 'Podcastmaker_Prompt_Sets' ) ) {
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-podcastmaker-prompt-sets.php';
		}

		$prompt_sets = new Podcastmaker_Prompt_Sets();
		$prompt_sets->reset_to_bundled_defaults();
	}

	/**
	 * Create or upgrade plugin tables.
	 */
	private static function create_tables() {
		global $wpdb;

		$usage_table_name = $wpdb->prefix . 'podcastmaker_usage_logs';
		$audio_events_table = $wpdb->prefix . 'podcastmaker_audio_events';
		$audio_library_table = $wpdb->prefix . 'podcastmaker_audio_library';
		$charset_collate = $wpdb->get_charset_collate();

		$usage_sql = "CREATE TABLE $usage_table_name (
			id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			user_id bigint(20) unsigned NOT NULL,
			action_type varchar(50) NOT NULL,
			model_used varchar(100) NOT NULL,
			char_count int(11) DEFAULT 0,
			duration int(11) DEFAULT 0,
			cost_estimated decimal(10, 6) DEFAULT 0.000000,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY user_id (user_id),
			KEY action_type (action_type)
		) $charset_collate;";

		$audio_sql = "CREATE TABLE $audio_events_table (
			id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			playback_session_id varchar(64) NOT NULL,
			visitor_id varchar(64) NOT NULL DEFAULT '',
			user_id bigint(20) unsigned NOT NULL DEFAULT 0,
			post_id bigint(20) unsigned NOT NULL DEFAULT 0,
			audio_key char(32) NOT NULL,
			audio_url text NOT NULL,
			audio_index smallint(5) unsigned NOT NULL DEFAULT 0,
			event_type varchar(20) NOT NULL,
			progress_bucket smallint(5) unsigned NOT NULL DEFAULT 0,
			progress_percent decimal(5,2) NOT NULL DEFAULT 0.00,
			duration_seconds decimal(10,2) NOT NULL DEFAULT 0.00,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			KEY playback_session_id (playback_session_id),
			KEY visitor_id (visitor_id),
			KEY post_id (post_id),
			KEY audio_key (audio_key),
			KEY event_type (event_type),
			KEY created_at (created_at)
		) $charset_collate;";

		$audio_library_sql = "CREATE TABLE $audio_library_table (
			id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			audio_key char(32) NOT NULL,
			audio_url text NOT NULL,
			attachment_id bigint(20) unsigned NOT NULL DEFAULT 0,
			script_post_id bigint(20) unsigned NOT NULL DEFAULT 0,
			source_post_id bigint(20) unsigned NOT NULL DEFAULT 0,
			audio_mode varchar(20) NOT NULL DEFAULT 'single',
			voice_id varchar(100) NOT NULL DEFAULT '',
			voice_label varchar(191) NOT NULL DEFAULT '',
			voice_summary_label varchar(191) NOT NULL DEFAULT '',
			speaker_map_json longtext NULL,
			speaker_count smallint(5) unsigned NOT NULL DEFAULT 1,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			updated_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY  (id),
			UNIQUE KEY audio_key (audio_key),
			KEY attachment_id (attachment_id),
			KEY source_post_id (source_post_id),
			KEY audio_mode (audio_mode),
			KEY voice_id (voice_id)
		) $charset_collate;";

		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $usage_sql );
		dbDelta( $audio_sql );
		dbDelta( $audio_library_sql );
	}
	
}
