namespace PlotLine.Models;

public static class StoryIntelligenceRunStatuses
{
    public const string Pending = "Pending";
    public const string Running = "Running";
    public const string Completed = "Completed";
    public const string CompletedWithWarnings = "CompletedWithWarnings";
    public const string Failed = "Failed";
    public const string Cancelled = "Cancelled";
}

public static class StoryIntelligenceFailureStages
{
    public const string DocumentRead = "DocumentRead";
    public const string ChapterStructure = "ChapterStructure";
    public const string SceneSplit = "SceneSplit";
    public const string SceneIntelligence = "SceneIntelligence";
    public const string Validation = "Validation";
    public const string Persistence = "Persistence";
    public const string DomainImport = "DomainImport";
}

public static class StoryIntelligencePipelineStages
{
    public const string Chapters = "Chapters";
    public const string SceneReview = "SceneReview";
    public const string SceneImport = "SceneImport";
    public const string CharacterReview = "CharacterReview";
    public const string CharacterImport = "CharacterImport";
    public const string LocationReview = "LocationReview";
    public const string LocationImport = "LocationImport";
    public const string AssetReview = "AssetReview";
    public const string AssetImport = "AssetImport";
    public const string RelationshipReview = "RelationshipReview";
    public const string RelationshipImport = "RelationshipImport";
    public const string KnowledgeReview = "KnowledgeReview";
    public const string KnowledgeImport = "KnowledgeImport";
    public const string Complete = "Complete";
}

public static class StoryIntelligencePipelineStatuses
{
    public const string InProgress = "InProgress";
    public const string NeedsReview = "NeedsReview";
    public const string Complete = "Complete";
}

public sealed class StoryIntelligenceBookPipelineState
{
    public int StoryIntelligenceBookPipelineID { get; init; }
    public int ProjectID { get; init; }
    public string ProjectName { get; init; } = string.Empty;
    public int BookID { get; init; }
    public string BookTitle { get; init; } = string.Empty;
    public string? BookSubtitle { get; init; }
    public string BookDisplayTitle => BookTitleFormatter.DisplayTitle(BookTitle, BookSubtitle);
    public string CurrentStage { get; init; } = StoryIntelligencePipelineStages.SceneReview;
    public string? LastCompletedStage { get; init; }
    public string? CurrentReviewStage { get; init; }
    public string Status { get; init; } = StoryIntelligencePipelineStatuses.NeedsReview;
    public DateTime? CompletedUtc { get; init; }
    public int? LastRunID { get; init; }
    public DateTime CreatedUtc { get; init; }
    public DateTime UpdatedUtc { get; init; }

    public bool IsComplete
        => string.Equals(Status, StoryIntelligencePipelineStatuses.Complete, StringComparison.OrdinalIgnoreCase)
            && string.Equals(LastCompletedStage, StoryIntelligencePipelineStages.KnowledgeImport, StringComparison.OrdinalIgnoreCase);
}

public sealed class StoryIntelligenceBookPipelineSaveRequest
{
    public int ProjectID { get; init; }
    public int BookID { get; init; }
    public string CurrentStage { get; init; } = StoryIntelligencePipelineStages.SceneReview;
    public string? LastCompletedStage { get; init; }
    public string? CurrentReviewStage { get; init; }
    public string Status { get; init; } = StoryIntelligencePipelineStatuses.NeedsReview;
    public DateTime? CompletedUtc { get; init; }
    public int? LastRunID { get; init; }
}

public sealed class StoryIntelligenceCommittedRunSummary
{
    public int StoryIntelligenceRunID { get; init; }
    public int UserID { get; init; }
    public int ProjectID { get; init; }
    public string ProjectName { get; init; } = string.Empty;
    public int BookID { get; init; }
    public string BookTitle { get; init; } = string.Empty;
    public string? BookSubtitle { get; init; }
    public int ChapterID { get; init; }
    public decimal ChapterNumber { get; init; }
    public string ChapterTitle { get; init; } = string.Empty;
    public string? SourceLabel { get; init; }
}

public sealed class StoryIntelligenceRunSaveRequest
{
    public int UserID { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public string Status { get; init; } = StoryIntelligenceRunStatuses.Completed;
    public string SourceType { get; init; } = "AdminDryRun";
    public string? SourceFileName { get; init; }
    public long? SourceFileSizeBytes { get; init; }
    public int? SourceWordCount { get; init; }
    public int? SourceCharacterCount { get; init; }
    public int? SourceParagraphCount { get; init; }
    public int? SourceChapterCount { get; init; }
    public int? SourceDetectedImagesCount { get; init; }
    public int? SourceDetectedTablesCount { get; init; }
    public int? SourceDetectedFootnotesCount { get; init; }
    public int? SourceDetectedCommentsCount { get; init; }
    public string PromptVersion { get; init; } = string.Empty;
    public string Model { get; init; } = string.Empty;
    public DateTime StartedUtc { get; init; }
    public DateTime? CompletedUtc { get; init; }
    public string? FailureStage { get; init; }
    public int? TotalInputTokens { get; init; }
    public int? TotalOutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? TotalDurationMs { get; init; }
    public decimal? EstimatedCostGBP { get; init; }
    public decimal? EstimatedCostUSD { get; init; }
    public string? PromptVersionsSummary { get; init; }
    public string? ErrorMessage { get; init; }
    public string? ErrorDetail { get; init; }
    public StoryIntelligenceChapterResultSaveRequest? ChapterResult { get; init; }
    public IReadOnlyList<StoryIntelligenceSceneResultSaveRequest> SceneResults { get; init; } = [];
}

public sealed class StoryIntelligenceRunQueueRequest
{
    public int UserID { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public decimal? ChapterNumber { get; init; }
    public string SourceType { get; init; } = "AdminText";
    public string SourceLabel { get; init; } = string.Empty;
    public string ChapterText { get; init; } = string.Empty;
    public string? SourceFileName { get; init; }
    public long? SourceFileSizeBytes { get; init; }
    public int? SourceWordCount { get; init; }
    public int? SourceCharacterCount { get; init; }
    public int? SourceParagraphCount { get; init; }
    public int? SourceChapterCount { get; init; }
    public string Model { get; init; } = string.Empty;
    public string PromptVersionsSummary { get; init; } = string.Empty;
}

public sealed class StoryIntelligenceChapterResultSaveRequest
{
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public decimal? ChapterNumber { get; init; }
    public string SourceLabel { get; init; } = string.Empty;
    public string PromptVersion { get; init; } = string.Empty;
    public string Model { get; init; } = string.Empty;
    public string RawResponseJson { get; init; } = string.Empty;
    public string OutputTextJson { get; init; } = string.Empty;
    public string ParsedJson { get; init; } = string.Empty;
    public int ValidationErrorsCount { get; init; }
    public int ValidationWarningsCount { get; init; }
    public int? InputTokens { get; init; }
    public int? OutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? DurationMs { get; init; }
}

public sealed class StoryIntelligenceSceneResultSaveRequest
{
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public int? SceneID { get; init; }
    public int TemporarySceneNumber { get; init; }
    public int? StartParagraph { get; init; }
    public int? EndParagraph { get; init; }
    public string SourceLabel { get; init; } = string.Empty;
    public string PromptVersion { get; init; } = string.Empty;
    public string Model { get; init; } = string.Empty;
    public string RawResponseJson { get; init; } = string.Empty;
    public string OutputTextJson { get; init; } = string.Empty;
    public string ParsedJson { get; init; } = string.Empty;
    public int ValidationErrorsCount { get; init; }
    public int ValidationWarningsCount { get; init; }
    public int? InputTokens { get; init; }
    public int? OutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? DurationMs { get; init; }
}

public sealed class StoryIntelligenceSavedRunListItem
{
    public int StoryIntelligenceRunID { get; init; }
    public DateTime CreatedUtc { get; init; }
    public int UserID { get; init; }
    public int? ProjectID { get; init; }
    public string? ProjectTitle { get; init; }
    public int? BookID { get; init; }
    public string? BookTitle { get; init; }
    public int? ChapterID { get; init; }
    public decimal? ChapterNumber { get; init; }
    public string Status { get; init; } = string.Empty;
    public string SourceType { get; init; } = string.Empty;
    public string? FailureStage { get; init; }
    public int SceneCount { get; init; }
    public int? TotalTokens { get; init; }
    public long? TotalDurationMs { get; init; }
    public decimal? EstimatedCostGBP { get; init; }
    public decimal? EstimatedCostUSD { get; init; }
    public int ValidationErrorsCount { get; init; }
    public int ValidationWarningsCount { get; init; }
}

public sealed class StoryIntelligenceSavedRun
{
    public int StoryIntelligenceRunID { get; init; }
    public int UserID { get; init; }
    public int? ProjectID { get; init; }
    public string? ProjectTitle { get; init; }
    public int? BookID { get; init; }
    public string? BookTitle { get; init; }
    public int? ChapterID { get; init; }
    public decimal? ChapterNumber { get; init; }
    public string Status { get; init; } = string.Empty;
    public string SourceType { get; init; } = string.Empty;
    public string? SourceText { get; init; }
    public string? SourceFileName { get; init; }
    public long? SourceFileSizeBytes { get; init; }
    public int? SourceWordCount { get; init; }
    public int? SourceCharacterCount { get; init; }
    public int? SourceParagraphCount { get; init; }
    public int? SourceChapterCount { get; init; }
    public int? SourceDetectedImagesCount { get; init; }
    public int? SourceDetectedTablesCount { get; init; }
    public int? SourceDetectedFootnotesCount { get; init; }
    public int? SourceDetectedCommentsCount { get; init; }
    public string? CurrentStage { get; init; }
    public string? CurrentMessage { get; init; }
    public int? TotalDetectedScenes { get; init; }
    public int? CompletedScenes { get; init; }
    public int? FailedScenes { get; init; }
    public DateTime? CancellationRequestedUtc { get; init; }
    public DateTime? CancelledUtc { get; init; }
    public string PromptVersion { get; init; } = string.Empty;
    public string? PromptVersionsSummary { get; init; }
    public string Model { get; init; } = string.Empty;
    public DateTime StartedUtc { get; init; }
    public DateTime? CompletedUtc { get; init; }
    public string? FailureStage { get; init; }
    public int? TotalInputTokens { get; init; }
    public int? TotalOutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? TotalDurationMs { get; init; }
    public decimal? EstimatedCostGBP { get; init; }
    public decimal? EstimatedCostUSD { get; init; }
    public string? ErrorMessage { get; init; }
    public string? ErrorDetail { get; init; }
    public DateTime CreatedUtc { get; init; }
    public DateTime UpdatedUtc { get; init; }
}

public sealed class StoryIntelligenceQueuedRun
{
    public int StoryIntelligenceRunID { get; init; }
    public int UserID { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public decimal? ChapterNumber { get; init; }
    public string Status { get; init; } = string.Empty;
    public string SourceType { get; init; } = string.Empty;
    public string? SourceLabel { get; init; }
    public string? SourceText { get; init; }
    public int? SourceWordCount { get; init; }
    public int? SourceCharacterCount { get; init; }
    public int? SourceParagraphCount { get; init; }
    public int? SourceChapterCount { get; init; }
    public string PromptVersion { get; init; } = string.Empty;
    public string? PromptVersionsSummary { get; init; }
    public string Model { get; init; } = string.Empty;
    public DateTime StartedUtc { get; init; }
    public DateTime? CompletedUtc { get; init; }
    public string? FailureStage { get; init; }
    public int? TotalInputTokens { get; init; }
    public int? TotalOutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? TotalDurationMs { get; init; }
    public decimal? EstimatedCostGBP { get; init; }
    public decimal? EstimatedCostUSD { get; init; }
    public string? ErrorMessage { get; init; }
    public string? ErrorDetail { get; init; }
    public string? CurrentStage { get; init; }
    public string? CurrentMessage { get; init; }
    public int? TotalDetectedScenes { get; init; }
    public int? CompletedScenes { get; init; }
    public int? FailedScenes { get; init; }
    public DateTime? CancellationRequestedUtc { get; init; }
    public DateTime? CancelledUtc { get; init; }
    public DateTime CreatedUtc { get; init; }
    public DateTime UpdatedUtc { get; init; }
    public bool CancellationRequested => CancellationRequestedUtc.HasValue;
}

public sealed class StoryIntelligenceSourceOption
{
    public int Value { get; init; }
    public string Text { get; init; } = string.Empty;
}

public sealed class StoryIntelligenceExistingChapterSource
{
    public int ProjectID { get; init; }
    public string ProjectName { get; init; } = string.Empty;
    public int BookID { get; init; }
    public string BookTitle { get; init; } = string.Empty;
    public string? BookSubtitle { get; init; }
    public int ChapterID { get; init; }
    public decimal ChapterNumber { get; init; }
    public string ChapterTitle { get; init; } = string.Empty;
    public string? SourceText { get; init; }
    public int? SourceWordCount { get; init; }
    public int? SourceCharacterCount { get; init; }

    public bool HasSourceText => !string.IsNullOrWhiteSpace(SourceText);
}

public sealed class StoryIntelligenceExistingChapterQueueForm
{
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
}

public sealed class StoryIntelligenceExistingChapterQueueViewModel
{
    public StoryIntelligenceExistingChapterQueueForm Form { get; init; } = new();
    public IReadOnlyList<StoryIntelligenceSourceOption> Projects { get; init; } = [];
    public IReadOnlyList<StoryIntelligenceSourceOption> Books { get; init; } = [];
    public IReadOnlyList<StoryIntelligenceSourceOption> Chapters { get; init; } = [];
    public StoryIntelligenceExistingChapterSource? SelectedChapter { get; init; }
    public string? WarningMessage { get; init; }
    public string? ErrorMessage { get; init; }
    public int? QueuedRunID { get; init; }
}

public sealed class StoryIntelligenceFullChapterTestForm
{
    public string? SourceLabel { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public string? ChapterStructureModel { get; init; }
    public string? SceneIntelligenceModel { get; init; }
    public string? ChapterText { get; init; }
}

public sealed class StoryIntelligenceFullChapterPreflight
{
    public int CharacterCount { get; init; }
    public int WordCount { get; init; }
    public int ParagraphCount { get; init; }
    public int EstimatedInputTokens { get; init; }
    public int EstimatedPipelineInputTokens { get; init; }
    public decimal? EstimatedInputCostUSD { get; init; }
    public decimal? EstimatedInputCostGBP { get; init; }
    public IReadOnlyList<string> Warnings { get; init; } = [];
}

public sealed class StoryIntelligenceFullChapterTestViewModel
{
    public StoryIntelligenceFullChapterTestForm Form { get; init; } = new();
    public IReadOnlyList<string> ModelOptions { get; init; } = [];
    public StoryIntelligenceFullChapterPreflight? Preflight { get; init; }
    public string? SourceFileName { get; init; }
    public long? SourceFileSizeBytes { get; init; }
    public string? ErrorMessage { get; init; }
}

public sealed class StoryIntelligenceSavedChapterResult
{
    public int ChapterResultID { get; init; }
    public int StoryIntelligenceRunID { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public decimal? ChapterNumber { get; init; }
    public string SourceLabel { get; init; } = string.Empty;
    public string PromptVersion { get; init; } = string.Empty;
    public string Model { get; init; } = string.Empty;
    public string RawResponseJson { get; init; } = string.Empty;
    public string OutputTextJson { get; init; } = string.Empty;
    public string ParsedJson { get; init; } = string.Empty;
    public int ValidationErrorsCount { get; init; }
    public int ValidationWarningsCount { get; init; }
    public int? InputTokens { get; init; }
    public int? OutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? DurationMs { get; init; }
    public DateTime CreatedUtc { get; init; }
}

public sealed class StoryIntelligenceSavedSceneResult
{
    public int SceneResultID { get; init; }
    public int StoryIntelligenceRunID { get; init; }
    public int? ChapterResultID { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public int? SceneID { get; init; }
    public int TemporarySceneNumber { get; init; }
    public int? StartParagraph { get; init; }
    public int? EndParagraph { get; init; }
    public string SourceLabel { get; init; } = string.Empty;
    public string PromptVersion { get; init; } = string.Empty;
    public string Model { get; init; } = string.Empty;
    public string RawResponseJson { get; init; } = string.Empty;
    public string OutputTextJson { get; init; } = string.Empty;
    public string ParsedJson { get; init; } = string.Empty;
    public int ValidationErrorsCount { get; init; }
    public int ValidationWarningsCount { get; init; }
    public int? InputTokens { get; init; }
    public int? OutputTokens { get; init; }
    public int? TotalTokens { get; init; }
    public long? DurationMs { get; init; }
    public DateTime CreatedUtc { get; init; }
}

public static class StoryIntelligenceImportCommitStatuses
{
    public const string Pending = "Pending";
    public const string Completed = "Completed";
    public const string Failed = "Failed";
}

public sealed class StoryIntelligenceImportCommit
{
    public int StoryIntelligenceImportCommitID { get; init; }
    public int StoryIntelligenceRunID { get; init; }
    public int? ProjectID { get; init; }
    public int? BookID { get; init; }
    public int? ChapterID { get; init; }
    public DateTime CommittedAt { get; init; }
    public int CommittedByUserID { get; init; }
    public int ScenesCreated { get; init; }
    public int MetricsCreated { get; init; }
    public string Status { get; init; } = string.Empty;
    public string? Notes { get; init; }
    public DateTime CreatedUtc { get; init; }
}

public sealed class StoryIntelligenceSceneImportItem
{
    public int SceneResultID { get; init; }
    public int TemporarySceneNumber { get; init; }
    public int? StartParagraph { get; init; }
    public int? EndParagraph { get; init; }
    public string SourceLabel { get; init; } = string.Empty;
    public SceneIntelligenceScene ParsedScene { get; init; } = new();
    public int? PovCharacterID { get; init; }
    public IReadOnlyList<int> PurposeTypeIDs { get; init; } = [];
    public IReadOnlyList<SceneMetricValue> Metrics { get; init; } = [];
    public string SceneTitle { get; init; } = string.Empty;
    public string? Summary { get; init; }
    public string? PurposeNotes { get; init; }
    public string? OutcomeNotes { get; init; }
    public string? ImportNoteText { get; init; }
}

public sealed class StoryIntelligenceImportCommitRequest
{
    public int StoryIntelligenceRunID { get; init; }
    public int UserID { get; init; }
    public int ProjectID { get; init; }
    public int BookID { get; init; }
    public int ChapterID { get; init; }
    public int TimeModeID { get; init; }
    public int TimeConfidenceID { get; init; }
    public int RevisionStatusID { get; init; }
    public int? TimelineNoteTypeID { get; init; }
    public string? ChapterSummary { get; init; }
    public int? ChapterPurposeID { get; init; }
    public IReadOnlyList<StoryIntelligenceSceneImportItem> Scenes { get; init; } = [];
    public IReadOnlyList<string> Warnings { get; init; } = [];
}

public sealed class StoryIntelligenceChapterImportReadiness
{
    public int ActiveSceneCount { get; init; }
    public bool CanCommit { get; init; }
    public string CommitMode { get; init; } = string.Empty;
    public string? BlockReason { get; init; }
}

public sealed class StoryIntelligenceImportCommitResult
{
    public bool Success { get; init; }
    public int? CommitID { get; init; }
    public int ScenesCreated { get; init; }
    public int MetricsCreated { get; init; }
    public string Message { get; init; } = string.Empty;
}
