<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @since 1.0.0
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
namespace Wisewand\Config;

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

class Audio_Voices {

	/**
	 * Get all available ElevenLabs voices for audio generation
	 *
	 * @return array Array of voice configurations
	 */
	public static function get_all_voices() {
		return array(
			// English Voices
			array(
				'id'          => '2EiwWnXFnvU5JabPnv8n',
				'name'        => 'Clyde',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'intense',
				'use_case'    => 'characters_animation',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'CwhRBWXzGAHq8TQ4Fs17',
				'name'        => 'Roger',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'classy',
				'use_case'    => 'conversational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'EXAVITQu4vr4xnSDxMaL',
				'name'        => 'Sarah',
				'language'    => 'en',
				'gender'      => 'female',
				'age'         => 'young',
				'style'       => 'professional',
				'use_case'    => 'entertainment_tv',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'FGY2WhTYpPnrIDTdsKH5',
				'name'        => 'Laura',
				'language'    => 'en',
				'gender'      => 'female',
				'age'         => 'young',
				'style'       => 'sassy',
				'use_case'    => 'social_media',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'GBv7mTt0atIp3Br8iCZE',
				'name'        => 'Thomas',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'young',
				'style'       => 'meditative',
				'use_case'    => 'narrative_story',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'IKne3meq5aSn9XLyUdCD',
				'name'        => 'Charlie',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'young',
				'style'       => 'hyped',
				'use_case'    => 'conversational',
				'accent'      => 'australian',
				'preview_url' => '',
			),
			array(
				'id'          => 'JBFqnCBsd6RMkjVDRZzb',
				'name'        => 'George',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'mature',
				'use_case'    => 'narrative_story',
				'accent'      => 'british',
				'preview_url' => '',
			),
			array(
				'id'          => 'N2lVS1w4EtoT3dr4eOWO',
				'name'        => 'Callum',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'neutral',
				'use_case'    => 'characters',
				'accent'      => 'neutral',
				'preview_url' => '',
			),
			array(
				'id'          => 'SAz9YHcvj6GT2YYXdXww',
				'name'        => 'River',
				'language'    => 'en',
				'gender'      => 'neutral',
				'age'         => 'middle_aged',
				'style'       => 'calm',
				'use_case'    => 'conversational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'SOYHLrjzK2X1ezoPC6cr',
				'name'        => 'Harry',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'young',
				'style'       => 'rough',
				'use_case'    => 'characters_animation',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'TX3LPaxmHKxFdv7VOQHJ',
				'name'        => 'Liam',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'young',
				'style'       => 'confident',
				'use_case'    => 'social_media',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'Xb7hH8MSUJpSbSDYk0k2',
				'name'        => 'Alice',
				'language'    => 'en',
				'gender'      => 'female',
				'age'         => 'middle_aged',
				'style'       => 'professional',
				'use_case'    => 'advertisement',
				'accent'      => 'british',
				'preview_url' => '',
			),
			array(
				'id'          => 'XrExE9yKIg1WjnnlVkGX',
				'name'        => 'Matilda',
				'language'    => 'en',
				'gender'      => 'female',
				'age'         => 'middle_aged',
				'style'       => 'upbeat',
				'use_case'    => 'informative_educational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'bIHbv24MWmeRgasZH58o',
				'name'        => 'Will',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'young',
				'style'       => 'chill',
				'use_case'    => 'conversational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'cgSgspJ2msm6clMCkdW9',
				'name'        => 'Jessica',
				'language'    => 'en',
				'gender'      => 'female',
				'age'         => 'young',
				'style'       => 'cute',
				'use_case'    => 'conversational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'cjVigY5qzO86Huf0OWal',
				'name'        => 'Eric',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'classy',
				'use_case'    => 'conversational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'iP95p4xoKVk53GoZ742B',
				'name'        => 'Chris',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'casual',
				'use_case'    => 'conversational',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'nPczCjzI2devNBz1zQrb',
				'name'        => 'Brian',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'classy',
				'use_case'    => 'social_media',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'onwK4e9ZLuTAKqWW03F9',
				'name'        => 'Daniel',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'formal',
				'use_case'    => 'informative_educational',
				'accent'      => 'british',
				'preview_url' => '',
			),
			array(
				'id'          => 'pFZP5JQG7iQjIQuC4Bku',
				'name'        => 'Lily',
				'language'    => 'en',
				'gender'      => 'female',
				'age'         => 'middle_aged',
				'style'       => 'warm',
				'use_case'    => 'narration',
				'accent'      => 'british',
				'preview_url' => '',
			),
			array(
				'id'          => 'pqHfZKP75CvOlQylNhV4',
				'name'        => 'Bill',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'old',
				'style'       => 'crisp',
				'use_case'    => 'advertisement',
				'accent'      => 'american',
				'preview_url' => '',
			),
			array(
				'id'          => 'FmJ4FDkdrYIKzBTruTkV',
				'name'        => 'David Hertel',
				'language'    => 'en',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'upbeat',
				'use_case'    => 'entertainment_tv',
				'accent'      => 'american',
				'preview_url' => '',
			),
			// French Voice
			array(
				'id'          => 'jSrrwmj13CIlQAXPpk4X',
				'name'        => 'Sebastien',
				'language'    => 'fr',
				'gender'      => 'male',
				'age'         => 'middle_aged',
				'style'       => 'casual',
				'use_case'    => 'social_media',
				'accent'      => 'standard',
				'preview_url' => '',
			),
		);
	}

	/**
	 * Get voices filtered by language
	 *
	 * @param string $language Language code (en, fr)
	 * @return array Filtered voices
	 */
	public static function get_voices_by_language( $language = 'en' ) {
		$all_voices = self::get_all_voices();

		return array_filter(
			$all_voices,
			function ( $voice ) use ( $language ) {
				return $voice['language'] === $language;
			}
		);
	}

	/**
	 * Get voice by ID
	 *
	 * @param string $voice_id Voice ID
	 * @return array|null Voice data or null if not found
	 */
	public static function get_voice( $voice_id ) {
		$all_voices = self::get_all_voices();

		foreach ( $all_voices as $voice ) {
			if ( $voice['id'] === $voice_id ) {
				return $voice;
			}
		}

		return null;
	}

	/**
	 * Get default voice ID (French Sebastien)
	 *
	 * @return string Default voice ID
	 */
	public static function get_default_voice_id() {
		return 'jSrrwmj13CIlQAXPpk4X'; // Sebastien (French)
	}

	/**
	 * Format voice display name with metadata
	 *
	 * @param array $voice Voice data
	 * @return string Formatted display name
	 */
	public static function format_voice_name( $voice ) {
		$gender_icon = $voice['gender'] === 'female' ? '👩' : ( $voice['gender'] === 'male' ? '👨' : '🧑' );
		$accent_flag = array(
			'american'   => '🇺🇸',
			'british'    => '🇬🇧',
			'australian' => '🇦🇺',
			'french'     => '🇫🇷',
		)[ $voice['accent'] ] ?? '';

		return sprintf(
			'%s %s %s - %s (%s)',
			$accent_flag,
			$gender_icon,
			$voice['name'],
			ucfirst( $voice['style'] ),
			ucfirst( str_replace( '_', ' ', $voice['use_case'] ) )
		);
	}
}
