using QuantEngine.Infrastructure.Repositories;

namespace QuantEngine.Core.Tests;

public class PostgresqlHistoryStoreTests
{
    [Fact]
    public void DomainColumnsExposeCanonicalDomains()
    {
        var domains = PostgresqlHistoryStore.GetDomainColumns();

        Assert.Contains("decision_result_history", domains.Keys);
        Assert.Contains("factor_output_history", domains.Keys);
        Assert.Contains("market_raw_history", domains.Keys);
        Assert.Contains("market_vs_engine_gap_history", domains.Keys);
        Assert.True(domains["decision_result_history"].Contains("decision_id"));
        Assert.True(domains["factor_output_history"].Contains("output_gate"));
    }

    [Fact]
    public void BuildInsertSqlUsesEngineHistoryPrefixAndNamedParameters()
    {
        var sql = PostgresqlHistoryStore.BuildInsertSql(
            "decision_result_history",
            new[] { "decision_id", "decided_at", "instrument_id", "action", "gate", "score", "source_version", "provenance" });

        Assert.Equal(
            "INSERT INTO engine_history.decision_result_history (decision_id, decided_at, instrument_id, action, gate, score, source_version, provenance) VALUES (@decision_id, @decided_at, @instrument_id, @action, @gate, @score, @source_version, @provenance)",
            sql);
    }

    [Fact]
    public void BuildSnapshotSqlUsesCreatedAtDescendingAndLimitParameter()
    {
        var sql = PostgresqlHistoryStore.BuildSnapshotSql("factor_output_history", 25);

        Assert.Equal(
            "SELECT * FROM engine_history.factor_output_history ORDER BY created_at DESC LIMIT @Limit",
            sql);
    }
}
