namespace PlotLine.Models;

public static class ManuscriptScanSources
{
    public const string WordCompanion = "WordCompanion";
}

public static class ManuscriptScanStatuses
{
    public const string NotStarted = "NotStarted";
    public const string Running = "Running";
    public const string Complete = "Complete";
    public const string Failed = "Failed";
}

public static class ManuscriptScanReviewStatuses
{
    public const string ScanComplete = "ScanComplete";
    public const string ReviewInProgress = "ReviewInProgress";
    public const string ReviewComplete = "ReviewComplete";
    public const string ReadyToImport = "ReadyToImport";
}

public sealed class ManuscriptScanCommand
{
    public int UserID { get; init; }
    public int OnboardingID { get; init; }
    public int ProjectID { get; init; }
    public int BookID { get; init; }
    public DateTime RequestedUtc { get; init; }
}

public sealed class ManuscriptScanProgress
{
    public int? UserID { get; init; }
    public int OnboardingID { get; init; }
    public string Message { get; init; } = string.Empty;
    public int? PercentComplete { get; init; }
    public int ChapterCount { get; init; }
    public int SceneCount { get; init; }
    public int CharacterCandidateCount { get; init; }
    public int TotalWordCount { get; init; }
}

public sealed class ManuscriptScanFailure
{
    public int? UserID { get; init; }
    public int OnboardingID { get; init; }
    public string Message { get; init; } = string.Empty;
}

public sealed class ManuscriptScanPreview
{
    public Guid PreviewID { get; init; } = Guid.NewGuid();
    public int UserID { get; init; }
    public int OnboardingID { get; init; }
    public int ProjectID { get; init; }
    public int BookID { get; init; }
    public string Source { get; init; } = ManuscriptScanSources.WordCompanion;
    public string? DocumentTitle { get; init; }
    public string? CompanionDocumentIdentifier { get; init; }
    public int TotalWordCount { get; init; }
    public int ChapterCount { get; init; }
    public int SceneCount { get; init; }
    public int CharacterCandidateCount { get; init; }
    public DateTime CreatedUtc { get; init; } = DateTime.UtcNow;
    public IReadOnlyList<ManuscriptScanChapterPreview> Chapters { get; init; } = [];
    public IReadOnlyList<ManuscriptScanScenePreview> Scenes { get; init; } = [];
    public IReadOnlyList<ManuscriptScanCharacterCandidatePreview> CharacterCandidates { get; init; } = [];
}

public sealed class ManuscriptScanChapterPreview
{
    public string TemporaryChapterKey { get; init; } = string.Empty;
    public int ChapterNumber { get; init; }
    public string Title { get; init; } = string.Empty;
    public int WordCount { get; init; }
    public string? ChapterText { get; init; }
    public int? StartPosition { get; init; }
    public int? ExistingChapterID { get; init; }
}

public sealed class ManuscriptScanScenePreview
{
    public string TemporarySceneKey { get; init; } = string.Empty;
    public string TemporaryChapterKey { get; init; } = string.Empty;
    public int SceneNumberWithinChapter { get; init; }
    public string? Title { get; init; }
    public int WordCount { get; init; }
    public string? OpeningTextPreview { get; init; }
    public int? StartPosition { get; init; }
    public int? ExistingSceneID { get; init; }
}

public sealed class ManuscriptScanCharacterCandidatePreview
{
    public string TemporaryCharacterKey { get; init; } = string.Empty;
    public string Name { get; init; } = string.Empty;
    public int MentionCount { get; init; }
    public int QualityScore { get; init; }
    public string Category { get; init; } = "PossibleCharacter";
    public string? Reason { get; init; }
    public bool IsExistingCharacterMatch { get; init; }
    public string? SuggestedImportance { get; init; }
}

public sealed class ManuscriptScanState
{
    public string Status { get; init; } = ManuscriptScanStatuses.NotStarted;
    public string ReviewStatus { get; init; } = ManuscriptScanReviewStatuses.ScanComplete;
    public string Message { get; init; } = string.Empty;
    public int? PercentComplete { get; init; }
    public int ChapterCount { get; init; }
    public int SceneCount { get; init; }
    public int CharacterCandidateCount { get; init; }
    public int TotalWordCount { get; init; }
    public Guid? PreviewID { get; init; }
    public DateTime? UpdatedUtc { get; init; }
}

public sealed class ManuscriptScanReviewDecision
{
    public Guid PreviewID { get; init; }
    public string Status { get; init; } = ManuscriptScanReviewStatuses.ScanComplete;
    public DateTime UpdatedUtc { get; init; } = DateTime.UtcNow;
    public IReadOnlyList<ManuscriptScanChapterReviewDecision> Chapters { get; init; } = [];
    public IReadOnlyList<ManuscriptScanSceneReviewDecision> Scenes { get; init; } = [];
    public IReadOnlyList<ManuscriptScanCharacterReviewDecision> Characters { get; init; } = [];
}

public sealed class ManuscriptScanChapterReviewDecision
{
    public string TemporaryChapterKey { get; init; } = string.Empty;
    public bool Include { get; init; }
    public string Title { get; init; } = string.Empty;
    public int ChapterNumber { get; init; }
}

public sealed class ManuscriptScanSceneReviewDecision
{
    public string TemporarySceneKey { get; init; } = string.Empty;
    public string TemporaryChapterKey { get; init; } = string.Empty;
    public bool Include { get; init; }
    public string? Title { get; init; }
    public int SceneNumberWithinChapter { get; init; }
    public bool AllowZeroWords { get; init; }
}

public sealed class ManuscriptScanCharacterReviewDecision
{
    public string TemporaryCharacterKey { get; init; } = string.Empty;
    public bool Include { get; init; }
    public string Name { get; init; } = string.Empty;
    public string Category { get; init; } = "PossibleCharacter";
    public int? ExistingCharacterID { get; init; }
}

public sealed class OnboardingManuscriptBuildResult
{
    public int BuildID { get; init; }
    public Guid PreviewID { get; init; }
    public int ProjectID { get; init; }
    public int BookID { get; init; }
    public int? ManuscriptDocumentID { get; init; }
    public string Status { get; init; } = "Built";
    public string MarkerStatus { get; init; } = "Pending";
    public int ChaptersCreated { get; init; }
    public int ScenesCreated { get; init; }
    public int CharactersCreated { get; init; }
    public int CharactersReused { get; init; }
    public int SceneAppearancesCreated { get; init; }
    public bool AlreadyBuilt { get; init; }
    public string Message { get; init; } = string.Empty;
    public IReadOnlyList<OnboardingBuildChapterMap> ChapterMappings { get; init; } = [];
    public IReadOnlyList<OnboardingBuildSceneMap> SceneMappings { get; init; } = [];
    public IReadOnlyList<OnboardingBuildCharacterMap> CharacterMappings { get; init; } = [];
    public string? MarkerWarning { get; init; }
}

public sealed class OnboardingBuildChapterMap
{
    public string TemporaryChapterKey { get; init; } = string.Empty;
    public int ChapterID { get; init; }
}

public sealed class OnboardingBuildSceneMap
{
    public string TemporarySceneKey { get; init; } = string.Empty;
    public int SceneID { get; init; }
}

public sealed class OnboardingBuildCharacterMap
{
    public string TemporaryCharacterKey { get; init; } = string.Empty;
    public int CharacterID { get; init; }
    public bool Created { get; init; }
}

public sealed class OnboardingBuildProgress
{
    public string Status { get; init; } = "Running";
    public string Message { get; init; } = string.Empty;
    public int? PercentComplete { get; init; }
    public int ChaptersCreated { get; init; }
    public int ScenesCreated { get; init; }
    public int CharactersCreated { get; init; }
    public int SceneAppearancesCreated { get; init; }
    public Guid? PreviewID { get; init; }
}
