namespace KArtSell.Modules.ModelOperations.FeedbackLoop;

public enum EvidenceClassification
{
    Source,
    Assumption,
    Unknown,
    DecisionRequired
}

public sealed record HypothesisEvidence(
    EvidenceClassification Classification,
    string Reference,
    string Statement,
    string ContentHash);

public sealed record ModelImprovementHypothesis(
    Guid HypothesisId,
    string ScopeKey,
    string BaseModelVersion,
    string ProblemStatement,
    string ProposedChange,
    string FalsificationCriterion,
    IReadOnlyList<HypothesisEvidence> SupportingEvidence,
    IReadOnlyList<HypothesisEvidence> CounterEvidence,
    string Owner,
    DateTimeOffset ExpiresAt)
{
    public IReadOnlyList<string> Validate(DateTimeOffset now)
    {
        var errors = new List<string>();
        if (HypothesisId == Guid.Empty) errors.Add("HYPOTHESIS_ID_REQUIRED");
        if (string.IsNullOrWhiteSpace(ScopeKey)) errors.Add("SCOPE_REQUIRED");
        if (string.IsNullOrWhiteSpace(BaseModelVersion)) errors.Add("BASE_MODEL_REQUIRED");
        if (string.IsNullOrWhiteSpace(ProblemStatement)) errors.Add("PROBLEM_REQUIRED");
        if (string.IsNullOrWhiteSpace(ProposedChange)) errors.Add("CHANGE_REQUIRED");
        if (string.IsNullOrWhiteSpace(FalsificationCriterion)) errors.Add("FALSIFICATION_TEST_REQUIRED");
        if (SupportingEvidence.Count == 0) errors.Add("SUPPORTING_EVIDENCE_REQUIRED");
        if (CounterEvidence.Count == 0) errors.Add("COUNTER_EVIDENCE_REQUIRED");
        if (SupportingEvidence.Concat(CounterEvidence).Any(x => x.Classification == EvidenceClassification.Unknown))
            errors.Add("UNKNOWN_EVIDENCE_MUST_BE_RESOLVED_OR_DOWNGRADED");
        if (SupportingEvidence.Concat(CounterEvidence).Any(x => x.Classification == EvidenceClassification.DecisionRequired))
            errors.Add("DECISION_REQUIRED_BLOCKS_EXPERIMENT");
        if (ExpiresAt <= now) errors.Add("HYPOTHESIS_EXPIRED");
        return errors;
    }
}
