namespace KArtSell.BuildingBlocks.DataIntegrity;

public enum DataQualityStatus
{
    Pass,
    Warn,
    Quarantined
}

/// <summary>
/// Point-in-time metadata shared by canonical data records.
/// PublishedAt controls the earliest market session in which the record may be used.
/// </summary>
public sealed record PitRecordMetadata(
    string SourceId,
    DateTimeOffset SourceEventTime,
    DateTimeOffset PublishedAt,
    DateTimeOffset IngestedAt,
    DateTimeOffset EffectiveFrom,
    DateTimeOffset? EffectiveTo,
    int RevisionNo,
    string ContentHash,
    string DatasetId,
    DataQualityStatus QualityStatus)
{
    public void EnsureValid()
    {
        if (string.IsNullOrWhiteSpace(SourceId)) throw new ArgumentException("SourceId is required.");
        if (string.IsNullOrWhiteSpace(ContentHash)) throw new ArgumentException("ContentHash is required.");
        if (string.IsNullOrWhiteSpace(DatasetId)) throw new ArgumentException("DatasetId is required.");
        if (RevisionNo < 0) throw new ArgumentOutOfRangeException(nameof(RevisionNo));
        if (IngestedAt < PublishedAt) throw new ArgumentException("IngestedAt cannot precede PublishedAt.");
        if (EffectiveTo is not null && EffectiveTo < EffectiveFrom)
            throw new ArgumentException("EffectiveTo cannot precede EffectiveFrom.");
    }

    public bool IsUsableAt(DateTimeOffset decisionCutoff)
        => QualityStatus is DataQualityStatus.Pass && PublishedAt <= decisionCutoff;
}
