using Dapper;
using QuantEngine.Core.Interfaces;
using QuantEngine.Infrastructure.Data;

namespace QuantEngine.Infrastructure.Repositories;

public sealed class LearningDatasetReader : ILearningDatasetReader
{
    private readonly IDbConnectionFactory _connectionFactory;

    public LearningDatasetReader(IDbConnectionFactory connectionFactory) => _connectionFactory = connectionFactory;

    public async Task<IReadOnlyList<IDictionary<string, object?>>> ReadTrainingExamplesAsync(int limit = 1000)
    {
        if (limit is < 1 or > 10000)
            throw new ArgumentOutOfRangeException(nameof(limit));

        using var connection = _connectionFactory.CreateConnection();
        var rows = await connection.QueryAsync(
            "SELECT * FROM engine_history.training_example_v1 ORDER BY decided_at DESC LIMIT @Limit",
            new { Limit = limit });
        return rows.Select(row => (IDictionary<string, object?>)row).ToList();
    }
}
