using FlatRender.StudioSvc.Domain.Enums;

namespace FlatRender.StudioSvc.Domain.Entities;

public class SavedProject
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public Guid TenantId { get; set; }
    public Guid UserId { get; set; }

    // Source template snapshot
    public Guid OriginalProjectId { get; set; }
    public string OriginalProjectName { get; set; } = default!;
    public Guid? OriginalContainerId { get; set; }
    public string? OriginalContainerSlug { get; set; }

    // Identity
    public string Name { get; set; } = default!;
    public string? Image { get; set; }
    public SavedProjectType Type { get; set; } = SavedProjectType.Draft;

    // Project metadata snapshot
    public int FrameRate { get; set; } = 30;
    public decimal ProjectDurationSec { get; set; }
    public string Resolution { get; set; } = "FullHD";
    public string ChooseMode { get; set; } = "FLEXIBLE";
    public decimal VipFactor { get; set; } = 1.0m;

    // Audio
    public Guid? MusicFileId { get; set; }
    public Guid? MusicTrackId { get; set; }
    public decimal MusicVolume { get; set; } = 0.7m;
    public Guid? VoiceoverFileId { get; set; }
    public decimal VoiceoverVolume { get; set; } = 1.0m;
    public bool VoiceoverRecordedInBrowser { get; set; }
    public decimal SfxVolume { get; set; } = 1.0m;
    public bool SfxEnabled { get; set; } = true;
    public string? AudioVisualizerMusicUrl { get; set; }
    public decimal? AudioVisualizerDurationSec { get; set; }

    // Customization
    public bool ManualColorPicker { get; set; }
    public Guid? SelectedPresetStoryId { get; set; }

    // State
    public string? LastEditStep { get; set; }
    public string EditState { get; set; } = "{}";

    public DateTime CreateDate { get; set; } = DateTime.UtcNow;
    public DateTime LastEditDate { get; set; } = DateTime.UtcNow;
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
    public DateTime? DeletedAt { get; set; }

    public ICollection<SavedScene> Scenes { get; set; } = [];
    public ICollection<SavedSharedColor> SharedColors { get; set; } = [];
    public ICollection<SavedSharedColorPreset> SharedColorPresets { get; set; } = [];
    public ICollection<SavedSharedLayer> SharedLayers { get; set; } = [];
}

public class SavedScene
{
    public long Id { get; set; }
    public Guid SavedProjectId { get; set; }
    public SavedProject SavedProject { get; set; } = default!;

    public Guid? OriginalSceneId { get; set; }
    public string Key { get; set; } = default!;
    public string? Title { get; set; }
    public string? Image { get; set; }
    public string? Demo { get; set; }
    public string? SceneColorSvg { get; set; }
    public string SceneType { get; set; } = "Normal";

    // Timing
    public int Sort { get; set; }
    public decimal SceneLengthSec { get; set; }
    public decimal? MinDurationSec { get; set; }
    public decimal? MaxDurationSec { get; set; }
    public decimal OverlapAtEndSec { get; set; }
    public bool CanHandleDuration { get; set; } = true;

    // Customization
    public bool ManualColorSelection { get; set; }
    public long? SelectedColorPresetId { get; set; }

    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

    public ICollection<SavedSceneContent> Contents { get; set; } = [];
    public ICollection<SavedSceneColor> Colors { get; set; } = [];
    public ICollection<SavedSceneColorPreset> ColorPresets { get; set; } = [];
    public ICollection<SavedSceneCharacter> Characters { get; set; } = [];
}

public class SavedSceneContent
{
    public long Id { get; set; }
    public long SavedSceneId { get; set; }
    public SavedScene SavedScene { get; set; } = default!;

    public string Key { get; set; } = default!;
    public string? Title { get; set; }
    public string? LocalizedTitle { get; set; }
    public string? Hint { get; set; }
    public string Type { get; set; } = default!;

    // Value
    public string? Value { get; set; }
    public Guid? ValueFileId { get; set; }
    public string? InsertedFileType { get; set; }
    public string? FileUrlCached { get; set; }
    public DateTime? FileUrlCachedAt { get; set; }

    // Text styling
    public string? FontFace { get; set; }
    public string? FontFaceName { get; set; }
    public int? FontSize { get; set; }
    public int? DefaultFontSize { get; set; }
    public string? DefaultFontFace { get; set; }
    public string? Justify { get; set; }
    public int PositionInContainer { get; set; }
    public int DirectionLayerValue { get; set; }
    public bool IsTextBox { get; set; }

    // AI
    public string? AiInputType { get; set; }

    // Design pattern
    public int? SelectedDp { get; set; }

    // Repeater
    public string? RepeaterItemKey { get; set; }
    public int? RepeaterIndex { get; set; }

    // State
    public bool IsFocused { get; set; }
    public string? Status { get; set; }
    public string? MappedList { get; set; }
    public string? Thumbnail { get; set; }

    public int Sort { get; set; }
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}

public class SavedSceneColor
{
    public long Id { get; set; }
    public long SavedSceneId { get; set; }
    public SavedScene SavedScene { get; set; } = default!;

    public string ElementKey { get; set; } = default!;
    public string? Title { get; set; }
    public string? Icon { get; set; }
    public string AttrValue { get; set; } = "fill";
    public string Value { get; set; } = default!;
    public bool IsSelected { get; set; } = true;
    public int Sort { get; set; }
}

public class SavedSceneColorPreset
{
    public long Id { get; set; }
    public long SavedSceneId { get; set; }
    public SavedScene SavedScene { get; set; } = default!;
    public bool IsSelected { get; set; }
    public int Sort { get; set; }

    public ICollection<SavedSceneColorPresetItem> Items { get; set; } = [];
}

public class SavedSceneColorPresetItem
{
    public long Id { get; set; }
    public long PresetId { get; set; }
    public SavedSceneColorPreset Preset { get; set; } = default!;
    public string ElementKey { get; set; } = default!;
    public string Value { get; set; } = default!;
    public int Sort { get; set; }
}

public class SavedSceneCharacter
{
    public long Id { get; set; }
    public long SavedSceneId { get; set; }
    public SavedScene SavedScene { get; set; } = default!;

    public Guid Key { get; set; }
    public string? Name { get; set; }
    public string? Icon { get; set; }

    public ICollection<SavedSceneCharacterController> Controllers { get; set; } = [];
}

public class SavedSceneCharacterController
{
    public long Id { get; set; }
    public long SavedSceneCharacterId { get; set; }
    public SavedSceneCharacter Character { get; set; } = default!;

    public string? Name { get; set; }
    public string Key { get; set; } = default!;
    public string Value { get; set; } = default!;
    public int Sort { get; set; }
}

public class SavedSharedColor
{
    public long Id { get; set; }
    public Guid SavedProjectId { get; set; }
    public SavedProject SavedProject { get; set; } = default!;

    public string ElementKey { get; set; } = default!;
    public string? Title { get; set; }
    public string? Icon { get; set; }
    public string AttrValue { get; set; } = "fill";
    public string Value { get; set; } = default!;
    public bool IsSelected { get; set; } = true;
    public int Sort { get; set; }
}

public class SavedSharedColorPreset
{
    public long Id { get; set; }
    public Guid SavedProjectId { get; set; }
    public SavedProject SavedProject { get; set; } = default!;
    public string? Name { get; set; }
    public bool IsSelected { get; set; }
    public int Sort { get; set; }

    public ICollection<SavedSharedColorPresetItem> Items { get; set; } = [];
}

public class SavedSharedColorPresetItem
{
    public long Id { get; set; }
    public long PresetId { get; set; }
    public SavedSharedColorPreset Preset { get; set; } = default!;
    public string ElementKey { get; set; } = default!;
    public string Value { get; set; } = default!;
    public int Sort { get; set; }
}

public class SavedSharedLayer
{
    public long Id { get; set; }
    public Guid SavedProjectId { get; set; }
    public SavedProject SavedProject { get; set; } = default!;

    public string Key { get; set; } = default!;
    public string? Title { get; set; }
    public string? LocalizedTitle { get; set; }
    public string? Hint { get; set; }
    public string Type { get; set; } = default!;

    public string? Value { get; set; }
    public Guid? ValueFileId { get; set; }
    public string? FileUrlCached { get; set; }
    public DateTime? FileUrlCachedAt { get; set; }

    public string? FontFace { get; set; }
    public string? FontFaceName { get; set; }
    public int? FontSize { get; set; }
    public int? DefaultFontSize { get; set; }
    public string? DefaultFontFace { get; set; }
    public string? Justify { get; set; }
    public int PositionInContainer { get; set; }
    public int DirectionLayerValue { get; set; }
    public bool IsTextBox { get; set; }

    public string? AiInputType { get; set; }
    public string? MappedList { get; set; }
    public string? Thumbnail { get; set; }

    public int? Width { get; set; }
    public int? Height { get; set; }
    public decimal? MinDurationSec { get; set; }
    public decimal? MaxDurationSec { get; set; }

    public bool IsFocused { get; set; }
    public bool IsFontChangeable { get; set; } = true;
    public bool IsFontSizeChangeable { get; set; } = true;
    public string? Status { get; set; }

    public int Sort { get; set; }
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
