package elevenlabs

// OutputFormat represents the audio output format for TTS requests.
type OutputFormat string

// Available output formats for ElevenLabs TTS API.
// See: https://elevenlabs.io/docs/api-reference/text-to-speech
const (
	// MP3 formats
	FormatMP3_22050_32  OutputFormat = "mp3_22050_32"  // 22.05kHz, 32kbps
	FormatMP3_44100_32  OutputFormat = "mp3_44100_32"  // 44.1kHz, 32kbps
	FormatMP3_44100_64  OutputFormat = "mp3_44100_64"  // 44.1kHz, 64kbps
	FormatMP3_44100_96  OutputFormat = "mp3_44100_96"  // 44.1kHz, 96kbps
	FormatMP3_44100_128 OutputFormat = "mp3_44100_128" // 44.1kHz, 128kbps (default)
	FormatMP3_44100_192 OutputFormat = "mp3_44100_192" // 44.1kHz, 192kbps

	// PCM formats
	FormatPCM_16000 OutputFormat = "pcm_16000" // 16kHz, 16-bit
	FormatPCM_22050 OutputFormat = "pcm_22050" // 22.05kHz, 16-bit
	FormatPCM_24000 OutputFormat = "pcm_24000" // 24kHz, 16-bit
	FormatPCM_44100 OutputFormat = "pcm_44100" // 44.1kHz, 16-bit

	// Opus/ulaw formats
	FormatULaw_8000 OutputFormat = "ulaw_8000" // 8kHz u-law
)

// DefaultOutputFormat is the default audio format used when none is specified.
const DefaultOutputFormat = FormatMP3_44100_128

// Model represents an ElevenLabs TTS model.
type Model string

// Available models for ElevenLabs TTS API.
const (
	// ModelTurboV2_5 is the latest and fastest model with improved multilingual support.
	ModelTurboV2_5 Model = "eleven_turbo_v2_5"

	// ModelMultilingualV2 is the most emotionally expressive model with 29 language support.
	ModelMultilingualV2 Model = "eleven_multilingual_v2"

	// ModelMonolingualV1 is the original English-only model.
	ModelMonolingualV1 Model = "eleven_monolingual_v1"

	// ModelTurboV2 is the previous generation turbo model.
	ModelTurboV2 Model = "eleven_turbo_v2"
)

// DefaultModel is the default TTS model used when none is specified.
const DefaultModel = ModelTurboV2_5

// VoiceSettings controls voice synthesis parameters.
type VoiceSettings struct {
	// Stability controls voice consistency (0.0-1.0).
	// Lower values = more expressive, higher values = more consistent.
	Stability float64 `json:"stability,omitempty"`

	// SimilarityBoost controls how closely the voice matches the original (0.0-1.0).
	// Higher values = closer to original voice.
	SimilarityBoost float64 `json:"similarity_boost,omitempty"`

	// Style controls the speaking style intensity (0.0-1.0).
	// Only available with multilingual_v2 model.
	Style float64 `json:"style,omitempty"`

	// UseSpeakerBoost enables speaker boost for clearer audio.
	UseSpeakerBoost bool `json:"use_speaker_boost,omitempty"`
}

// DefaultVoiceSettings returns sensible default voice settings.
func DefaultVoiceSettings() VoiceSettings {
	return VoiceSettings{
		Stability:       0.5,
		SimilarityBoost: 0.75,
		Style:           0.0,
		UseSpeakerBoost: true,
	}
}

// TextToSpeechRequest represents a request to generate speech from text.
type TextToSpeechRequest struct {
	// Text is the text to convert to speech (required).
	Text string `json:"text"`

	// ModelID is the TTS model to use (optional, defaults to turbo_v2_5).
	ModelID Model `json:"model_id,omitempty"`

	// VoiceSettings controls synthesis parameters (optional).
	VoiceSettings *VoiceSettings `json:"voice_settings,omitempty"`

	// PreviousText is text that came before for context (optional).
	PreviousText string `json:"previous_text,omitempty"`

	// NextText is text that comes after for context (optional).
	NextText string `json:"next_text,omitempty"`
}

// Voice represents an ElevenLabs voice.
type Voice struct {
	// VoiceID is the unique identifier for this voice.
	VoiceID string `json:"voice_id"`

	// Name is the display name of the voice.
	Name string `json:"name"`

	// Category is the voice category (e.g., "premade", "cloned", "generated").
	Category string `json:"category"`

	// Description is an optional description of the voice.
	Description string `json:"description,omitempty"`

	// Labels are metadata tags for the voice.
	Labels map[string]string `json:"labels,omitempty"`

	// PreviewURL is a URL to preview audio for this voice.
	PreviewURL string `json:"preview_url,omitempty"`

	// Settings are the default voice settings.
	Settings *VoiceSettings `json:"settings,omitempty"`
}

// VoicesResponse represents the response from the list voices endpoint.
type VoicesResponse struct {
	Voices []Voice `json:"voices"`
}

// ErrorResponse represents an error response from the ElevenLabs API.
type ErrorResponse struct {
	Detail struct {
		Status  string `json:"status"`
		Message string `json:"message"`
	} `json:"detail"`
}

// VoiceDesignRequest represents a request to design a new voice from a text prompt.
type VoiceDesignRequest struct {
	// VoiceDescription describes the desired voice characteristics (20-1000 chars).
	// Example: "A warm, authoritative American male in his 40s with a calm, measured pace."
	VoiceDescription string `json:"voice_description"`

	// Text is the preview text to generate (100-1000 chars, optional).
	// If empty and AutoGenerateText is true, text will be auto-generated.
	Text string `json:"text,omitempty"`

	// AutoGenerateText generates preview text if Text is empty.
	AutoGenerateText bool `json:"auto_generate_text,omitempty"`

	// ModelID is the voice design model (optional, defaults to eleven_multilingual_ttv_v2).
	ModelID string `json:"model_id,omitempty"`

	// Loudness controls volume (-1 to 1, default 0.5).
	Loudness float64 `json:"loudness,omitempty"`

	// GuidanceScale controls prompt adherence (default 5).
	// Higher = more adherence to prompt but may reduce audio quality.
	GuidanceScale float64 `json:"guidance_scale,omitempty"`
}

// VoiceDesignResponse represents the response from the voice design endpoint.
type VoiceDesignResponse struct {
	// Previews contains the generated voice previews (typically 3).
	Previews []VoicePreview `json:"previews"`

	// Text is the preview text that was used.
	Text string `json:"text"`
}

// VoicePreview represents a single generated voice preview.
type VoicePreview struct {
	// AudioBase64 is the base64-encoded MP3 audio sample.
	AudioBase64 string `json:"audio_base_64"`

	// GeneratedVoiceID is the temporary ID for saving this voice.
	GeneratedVoiceID string `json:"generated_voice_id"`

	// MediaType is the audio format (e.g., "audio/mpeg").
	MediaType string `json:"media_type"`

	// DurationSecs is the preview audio duration.
	DurationSecs float64 `json:"duration_secs"`
}

// SaveVoiceRequest represents a request to save a generated voice preview.
type SaveVoiceRequest struct {
	// VoiceName is the display name for the saved voice.
	VoiceName string `json:"voice_name"`

	// VoiceDescription is an optional description.
	VoiceDescription string `json:"voice_description,omitempty"`

	// GeneratedVoiceID is the ID from VoicePreview.
	GeneratedVoiceID string `json:"generated_voice_id"`

	// Labels are optional metadata tags.
	Labels map[string]string `json:"labels,omitempty"`
}

// SaveVoiceResponse represents the response from saving a voice.
type SaveVoiceResponse struct {
	// VoiceID is the permanent voice ID for TTS use.
	VoiceID string `json:"voice_id"`
}
