using System.Text.Json;
using Tiku.Application.Catalog;
using Tiku.Domain.Content;
using Tiku.Application.QuestionBanks;

namespace Tiku.Application.Content;

public sealed record ContentManagementActor(Guid TenantId, Guid UserId);

public sealed record ContentManagementFilter(
    Guid? RegionId = null,
    Guid? EntryId = null,
    Guid? NodeId = null,
    Guid? CollectionId = null,
    string? ParentId = null,
    string? EntryType = null,
    string? CollectionType = null,
    string? Mode = null,
    string? MarkerType = null,
    string? Keyword = null,
    bool IncludeInactive = false,
    int? Limit = null);

public sealed record UpsertContentEntryCommand(
    Guid? Id,
    Guid? RegionId,
    string? LegacyId,
    string? EntryKey,
    string Name,
    string? EntryType,
    string? Icon,
    string? Route,
    string? Description,
    string? Visibility,
    JsonElement AccessRules,
    JsonElement LayoutConfig,
    int? Order,
    bool? IsActive);

public sealed record UpsertContentNodeCommand(
    Guid? Id,
    Guid EntryId,
    Guid? RegionId,
    Guid? ParentId,
    string? LegacyId,
    string? NodeKey,
    string Name,
    string? NodeType,
    string? MarkerType,
    JsonElement MarkerConfig,
    int? Order,
    bool? IsActive,
    bool? IsSelectable,
    bool? IsLeaf,
    JsonElement AccessRules,
    JsonElement Metadata);

public sealed record UpsertQuestionCollectionCommand(
    Guid? Id,
    Guid? RegionId,
    Guid? EntryId,
    Guid? NodeId,
    Guid? SubjectId,
    Guid? CategoryId,
    Guid? QuestionBankId,
    string? LegacyId,
    string Name,
    string? CollectionType,
    string? SourceType,
    JsonElement Filters,
    decimal? TotalScore,
    int? DurationMinutes,
    string? Status,
    int? Order,
    JsonElement AccessRules,
    JsonElement Metadata);

public sealed record ReplaceCollectionItemsCommand(
    Guid CollectionId,
    IReadOnlyCollection<CollectionQuestionCommand> Questions);

public sealed record CollectionQuestionCommand(
    QuestionLocator Locator,
    string? SectionKey,
    int? Order,
    decimal? Score,
    bool? Required,
    JsonElement Metadata);

public sealed record UpsertPracticeBlueprintCommand(
    Guid? Id,
    Guid? RegionId,
    Guid? EntryId,
    Guid? NodeId,
    Guid? CollectionId,
    string? LegacyId,
    string Name,
    string? Mode,
    string? AssemblyType,
    int? QuestionLimit,
    int? DurationMinutes,
    decimal? TotalScore,
    decimal? PassScore,
    JsonElement Sections,
    JsonElement Rules,
    JsonElement AccessRules,
    string? Status,
    int? Order);

public sealed record ContentEntryManagementItem(
    Guid Id,
    Guid? RegionId,
    string? LegacyId,
    string EntryKey,
    string Name,
    ContentEntryType EntryType,
    string? Icon,
    string? Route,
    string? Description,
    ContentVisibility Visibility,
    JsonElement AccessRules,
    JsonElement LayoutConfig,
    int Order,
    bool IsActive,
    DateTimeOffset CreatedAt,
    DateTimeOffset? UpdatedAt);

public sealed record ContentNodeManagementItem(
    Guid Id,
    Guid EntryId,
    Guid? RegionId,
    Guid? ParentId,
    string? LegacyId,
    string? NodeKey,
    string Name,
    ContentNodeType NodeType,
    ContentMarkerType? MarkerType,
    JsonElement MarkerConfig,
    string? Path,
    int Depth,
    int Order,
    bool IsActive,
    bool IsSelectable,
    bool IsLeaf,
    JsonElement AccessRules,
    JsonElement Metadata,
    DateTimeOffset CreatedAt,
    DateTimeOffset? UpdatedAt);

public sealed record QuestionCollectionManagementItem(
    Guid Id,
    Guid? RegionId,
    Guid? EntryId,
    Guid? NodeId,
    Guid? SubjectId,
    Guid? CategoryId,
    Guid? QuestionBankId,
    string? LegacyId,
    string Name,
    QuestionCollectionType CollectionType,
    QuestionCollectionSourceType SourceType,
    JsonElement Filters,
    int QuestionCount,
    decimal? TotalScore,
    int? DurationMinutes,
    ContentStatus Status,
    int Order,
    JsonElement AccessRules,
    JsonElement Metadata,
    DateTimeOffset CreatedAt,
    DateTimeOffset? UpdatedAt);

public sealed record QuestionCollectionItemManagementItem(
    Guid Id,
    Guid CollectionId,
    Guid QuestionId,
    QuestionLocator Locator,
    string? SectionKey,
    int Order,
    decimal? Score,
    bool Required,
    JsonElement Metadata);

public sealed record PracticeBlueprintManagementItem(
    Guid Id,
    Guid? RegionId,
    Guid? EntryId,
    Guid? NodeId,
    Guid? CollectionId,
    string? LegacyId,
    string Name,
    PracticeMode Mode,
    PracticeAssemblyType AssemblyType,
    int? QuestionLimit,
    int? DurationMinutes,
    decimal? TotalScore,
    decimal? PassScore,
    JsonElement Sections,
    JsonElement Rules,
    JsonElement AccessRules,
    ContentStatus Status,
    int Order,
    DateTimeOffset CreatedAt,
    DateTimeOffset? UpdatedAt);

public sealed record ContentManagementResult<TItem>(TItem Item);

public sealed record CollectionItemsReplaceResult(
    Guid CollectionId,
    int QuestionCount,
    IReadOnlyCollection<QuestionCollectionItemManagementItem> Items);

public sealed record ImportFieldSpec(
    string Field,
    string Label,
    bool Required,
    IReadOnlyCollection<string> Aliases,
    string Description,
    JsonElement Example);

public sealed record ImportFieldMappingItem(
    string ImportType,
    string Title,
    string Description,
    IReadOnlyCollection<ImportFieldSpec> Fields,
    IReadOnlyCollection<string> RequiredFields);

public sealed record ImportTemplateItem(
    string ImportType,
    string Format,
    string FileName,
    string MimeType,
    string ContentBase64,
    string ContentPreview,
    IReadOnlyCollection<ImportFieldSpec> Fields);

public sealed class ContentManagementException(string message, string code) : Exception(message)
{
    public string Code { get; } = code;
}
