namespace PlotLine.Models;

public static class StoryImportResolutionStatuses
{
    public const string Matched = "Matched";
    public const string NewCharacter = "New Character";
    public const string NewLocation = "New Location";
    public const string NewAsset = "New Asset";
    public const string PossibleMatch = "Possible Match";
    public const string PossibleDuplicate = "Possible Duplicate";
    public const string PossibleDuplicateAliasCandidate = "Possible Duplicate / Alias Candidate";
    public const string Ambiguous = "Ambiguous";
    public const string UnresolvedPersonReference = "Unresolved Person Reference";
    public const string GenericReference = "Generic Reference";
    public const string Ignored = "Ignored";
}

public sealed class StoryIntelligenceImportPreview
{
    public IReadOnlyList<StoryImportEntityResolution> Characters { get; init; } = [];
    public IReadOnlyList<StoryImportEntityResolution> Locations { get; init; } = [];
    public IReadOnlyList<StoryImportEntityResolution> Assets { get; init; } = [];
    public IReadOnlyList<StoryImportRelationshipPreview> Relationships { get; init; } = [];
    public IReadOnlyList<StoryImportKnowledgePreview> Knowledge { get; init; } = [];
    public IReadOnlyList<StoryImportKnowledgePreview> UnresolvedKnowledge { get; init; } = [];
    public IReadOnlyList<StoryImportTimelinePreview> Timeline { get; init; } = [];
    public IReadOnlyList<string> Warnings { get; init; } = [];
}

public sealed class StoryImportEntityResolution
{
    public string InputName { get; init; } = string.Empty;
    public string Status { get; init; } = string.Empty;
    public string EntityType { get; init; } = string.Empty;
    public string? MatchedName { get; init; }
    public int? MatchedID { get; init; }
    public decimal? Confidence { get; init; }
    public IReadOnlyList<StoryImportResolutionCandidate> Candidates { get; init; } = [];
    public string? Notes { get; init; }
    public string ConfidenceBand => ConfidenceToBand(Confidence);
    public int? ConfidencePercent => Confidence.HasValue ? (int)Math.Round(Confidence.Value * 100m) : null;

    private static string ConfidenceToBand(decimal? confidence)
        => confidence switch
        {
            >= 0.90m => "High",
            >= 0.70m => "Medium",
            null => "Unknown",
            _ => "Low"
        };
}

public sealed class StoryImportResolutionCandidate
{
    public int EntityID { get; init; }
    public string Name { get; init; } = string.Empty;
    public string MatchReason { get; init; } = string.Empty;
}

public sealed class StoryImportRelationshipPreview
{
    public string Action { get; init; } = string.Empty;
    public string CharacterA { get; init; } = string.Empty;
    public string CharacterB { get; init; } = string.Empty;
    public string Signal { get; init; } = string.Empty;
    public IReadOnlyList<int> SupportingSceneNumbers { get; init; } = [];
    public decimal? Confidence { get; init; }
    public int? ConfidencePercent => Confidence.HasValue ? (int)Math.Round(Confidence.Value * 100m) : null;
}

public sealed class StoryImportKnowledgePreview
{
    public string Action { get; init; } = "Would Update Knowledge";
    public string Character { get; init; } = string.Empty;
    public string KnowledgeItem { get; init; } = string.Empty;
    public string OldState { get; init; } = "Unknown";
    public string NewState { get; init; } = string.Empty;
    public decimal? Confidence { get; init; }
    public int? ConfidencePercent => Confidence.HasValue ? (int)Math.Round(Confidence.Value * 100m) : null;
}

public sealed class StoryImportTimelinePreview
{
    public string Action { get; init; } = "Timeline Clue";
    public int? SceneNumber { get; init; }
    public string SceneDate { get; init; } = string.Empty;
    public decimal? Confidence { get; init; }
    public int? ConfidencePercent => Confidence.HasValue ? (int)Math.Round(Confidence.Value * 100m) : null;
}
