using System.Text.Json.Serialization;

namespace SaasMediaWorker.Models;

/// <summary>
/// Redis kuyruğundan alınan video üretim job payload'ı.
/// NestJS VideoGenerationProducer tarafından JSON olarak yazılır.
/// </summary>
public class VideoGenerationJob
{
    [JsonPropertyName("jobId")]
    public string JobId { get; set; } = string.Empty;

    [JsonPropertyName("projectId")]
    public string ProjectId { get; set; } = string.Empty;

    [JsonPropertyName("renderJobId")]
    public string RenderJobId { get; set; } = string.Empty;

    [JsonPropertyName("scriptJson")]
    public ScriptPayload? ScriptJson { get; set; }

    [JsonPropertyName("language")]
    public string Language { get; set; } = "tr";

    [JsonPropertyName("aspectRatio")]
    public string AspectRatio { get; set; } = "PORTRAIT_9_16";

    [JsonPropertyName("videoStyle")]
    public string VideoStyle { get; set; } = "CINEMATIC";

    [JsonPropertyName("targetDuration")]
    public int TargetDuration { get; set; } = 60;

    [JsonPropertyName("scenes")]
    public List<ScenePayload> Scenes { get; set; } = new();

    [JsonPropertyName("timestamp")]
    public string Timestamp { get; set; } = string.Empty;
}

public class ScriptPayload
{
    [JsonPropertyName("metadata")]
    public ScriptMetadata? Metadata { get; set; }

    [JsonPropertyName("scenes")]
    public List<ScriptScene> Scenes { get; set; } = new();

    [JsonPropertyName("musicPrompt")]
    public string MusicPrompt { get; set; } = string.Empty;

    [JsonPropertyName("musicStyle")]
    public string MusicStyle { get; set; } = string.Empty;

    [JsonPropertyName("musicTechnical")]
    public MusicTechnicalPayload? MusicTechnical { get; set; }

    [JsonPropertyName("ambientSoundPrompts")]
    public List<string> AmbientSoundPrompts { get; set; } = new();

    [JsonPropertyName("voiceStyle")]
    public string VoiceStyle { get; set; } = string.Empty;
}

public class MusicTechnicalPayload
{
    [JsonPropertyName("bpm")]
    public int Bpm { get; set; }

    [JsonPropertyName("key")]
    public string? Key { get; set; }

    [JsonPropertyName("instruments")]
    public List<string> Instruments { get; set; } = new();

    [JsonPropertyName("emotionalArc")]
    public string EmotionalArc { get; set; } = string.Empty;
}

public class ScriptMetadata
{
    [JsonPropertyName("title")]
    public string Title { get; set; } = string.Empty;

    [JsonPropertyName("description")]
    public string Description { get; set; } = string.Empty;

    [JsonPropertyName("totalDurationSeconds")]
    public double TotalDurationSeconds { get; set; }

    [JsonPropertyName("language")]
    public string Language { get; set; } = string.Empty;

    [JsonPropertyName("hashtags")]
    public List<string> Hashtags { get; set; } = new();
}

public class ScriptScene
{
    [JsonPropertyName("order")]
    public int Order { get; set; }

    [JsonPropertyName("title")]
    public string? Title { get; set; }

    [JsonPropertyName("narrationText")]
    public string NarrationText { get; set; } = string.Empty;

    [JsonPropertyName("visualPrompt")]
    public string VisualPrompt { get; set; } = string.Empty;

    [JsonPropertyName("subtitleText")]
    public string SubtitleText { get; set; } = string.Empty;

    [JsonPropertyName("durationSeconds")]
    public double DurationSeconds { get; set; }

    [JsonPropertyName("transitionType")]
    public string TransitionType { get; set; } = "CUT";
}

public class ScenePayload
{
    [JsonPropertyName("id")]
    public string Id { get; set; } = string.Empty;

    [JsonPropertyName("order")]
    public int Order { get; set; }

    [JsonPropertyName("narrationText")]
    public string NarrationText { get; set; } = string.Empty;

    [JsonPropertyName("visualPrompt")]
    public string VisualPrompt { get; set; } = string.Empty;

    [JsonPropertyName("subtitleText")]
    public string SubtitleText { get; set; } = string.Empty;

    [JsonPropertyName("duration")]
    public double Duration { get; set; }

    [JsonPropertyName("transitionType")]
    public string TransitionType { get; set; } = "CUT";

    [JsonPropertyName("ambientSoundPrompt")]
    public string? AmbientSoundPrompt { get; set; }

    [JsonPropertyName("imagePath")]
    public string? ImagePath { get; set; }

    [JsonPropertyName("ttsProvider")]
    public string? TtsProvider { get; set; }
}

/// <summary>
/// Render pipeline'ının ürettiği medya dosyası referansı.
/// </summary>
public class GeneratedMediaFile
{
    public string SceneId { get; set; } = string.Empty;
    public int SceneOrder { get; set; }
    public MediaFileType Type { get; set; }
    public string LocalPath { get; set; } = string.Empty;
    public string? S3Url { get; set; }
    public string? S3Key { get; set; }
    public long FileSizeBytes { get; set; }
    public double? DurationSeconds { get; set; }
    public string MimeType { get; set; } = string.Empty;
    public string? AiProvider { get; set; }
}

public enum MediaFileType
{
    VideoClip,
    AudioNarration,
    AudioMusic,
    AudioAmbient,
    Subtitle,
    Thumbnail,
    FinalVideo
}
