using System.Text.Json;
using Tiku.Domain.Common;

namespace Tiku.Domain.Content;

public sealed class ContentEntry : AuditableTenantEntity
{
    public Guid? RegionId { get; set; }
    public string? LegacyId { get; set; }
    public string EntryKey { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public ContentEntryType EntryType { get; set; } = ContentEntryType.QuestionPractice;
    public string? Icon { get; set; }
    public string? Route { get; set; }
    public string? Description { get; set; }
    public ContentVisibility Visibility { get; set; } = ContentVisibility.Public;
    public JsonElement AccessRules { get; set; } = JsonDefaults.Object();
    public JsonElement LayoutConfig { get; set; } = JsonDefaults.Object();
    public int SortOrder { get; set; }
    public bool IsActive { get; set; } = true;
    public Guid? CreatedBy { get; set; }
    public Guid? UpdatedBy { get; set; }
}

public enum ContentEntryType
{
    QuestionPractice,
    Vocabulary,
    Handbook,
    Scoreline,
    Resource,
    AiReport,
    Custom
}

public enum ContentVisibility
{
    Public,
    Members,
    Svip,
    Hidden
}

public sealed class ContentNode : AuditableTenantEntity
{
    public Guid EntryId { get; set; }
    public Guid? RegionId { get; set; }
    public Guid? ParentId { get; set; }
    public string? LegacyId { get; set; }
    public string? NodeKey { get; set; }
    public string Name { get; set; } = string.Empty;
    public ContentNodeType NodeType { get; set; } = ContentNodeType.Category;
    public ContentMarkerType? MarkerType { get; set; }
    public JsonElement MarkerConfig { get; set; } = JsonDefaults.Object();
    public string? Path { get; set; }
    public int Depth { get; set; }
    public int SortOrder { get; set; }
    public bool IsActive { get; set; } = true;
    public bool IsSelectable { get; set; } = true;
    public bool IsLeaf { get; set; }
    public JsonElement AccessRules { get; set; } = JsonDefaults.Object();
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
    public Guid? CreatedBy { get; set; }
    public Guid? UpdatedBy { get; set; }
}

public enum ContentNodeType
{
    Category,
    Subject,
    Chapter,
    Paper,
    School,
    Major,
    ExamTarget,
    ResourceGroup,
    Custom
}

public enum ContentMarkerType
{
    School,
    Major,
    Subject,
    ExamTrack,
    CoursePackage,
    SalesIntent,
    Custom
}

public sealed class QuestionCollection : AuditableTenantEntity
{
    public Guid? RegionId { get; set; }
    public Guid? EntryId { get; set; }
    public Guid? NodeId { get; set; }
    public Guid? SubjectId { get; set; }
    public Guid? CategoryId { get; set; }
    public Guid? QuestionBankId { get; set; }
    public string? LegacyId { get; set; }
    public string Name { get; set; } = string.Empty;
    public QuestionCollectionType CollectionType { get; set; } = QuestionCollectionType.Dynamic;
    public QuestionCollectionSourceType SourceType { get; set; } = QuestionCollectionSourceType.Filters;
    public JsonElement Filters { get; set; } = JsonDefaults.Object();
    public JsonElement AccessRules { get; set; } = JsonDefaults.Object();
    public int QuestionCount { get; set; }
    public decimal? TotalScore { get; set; }
    public int? DurationMinutes { get; set; }
    public ContentStatus Status { get; set; } = ContentStatus.Active;
    public int SortOrder { get; set; }
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
    public Guid? CreatedBy { get; set; }
    public Guid? UpdatedBy { get; set; }
}

public enum QuestionCollectionType
{
    Dynamic,
    Manual,
    Paper,
    Chapter,
    MockExam
}

public enum QuestionCollectionSourceType
{
    Filters,
    ManualQuestions,
    NodeDescendants,
    Category,
    Subject,
    QuestionBank
}

public enum ContentStatus
{
    Draft,
    Active,
    Archived
}

public sealed class QuestionCollectionItem : AuditableTenantEntity
{
    public Guid CollectionId { get; set; }
    public Guid QuestionReferenceId { get; set; }
    public Guid QuestionOwnerTenantId { get; set; }
    public Guid QuestionId { get; set; }
    public string? SectionKey { get; set; }
    public int SortOrder { get; set; }
    public decimal? Score { get; set; }
    public bool Required { get; set; } = true;
    public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

public sealed class PracticeBlueprint : AuditableTenantEntity
{
    public Guid? RegionId { get; set; }
    public Guid? EntryId { get; set; }
    public Guid? NodeId { get; set; }
    public Guid? CollectionId { get; set; }
    public string? LegacyId { get; set; }
    public string Name { get; set; } = string.Empty;
    public PracticeMode Mode { get; set; } = PracticeMode.Sequential;
    public PracticeAssemblyType AssemblyType { get; set; } = PracticeAssemblyType.Collection;
    public int? QuestionLimit { get; set; }
    public int? DurationMinutes { get; set; }
    public decimal? TotalScore { get; set; }
    public decimal? PassScore { get; set; }
    public JsonElement Sections { get; set; } = JsonDefaults.Array();
    public JsonElement Rules { get; set; } = JsonDefaults.Object();
    public JsonElement AccessRules { get; set; } = JsonDefaults.Object();
    public ContentStatus Status { get; set; } = ContentStatus.Active;
    public int SortOrder { get; set; }
    public Guid? CreatedBy { get; set; }
    public Guid? UpdatedBy { get; set; }
}

public enum PracticeMode
{
    Sequential,
    Random,
    MockExam,
    Paper,
    WrongReview,
    FavoriteReview
}

public enum PracticeAssemblyType
{
    Collection,
    NodeDescendants,
    Manual,
    Filters
}