using System.Data;
using Insight.Database;

namespace PlanTempus.Core.Configurations.SmartConfigProvider.Repositories;
public class PostgresConfigurationRepository : IConfigurationRepository
{
    private IDbConnection _connection;
    public string ConnectionString { get; set; }


    public PostgresConfigurationRepository(string connectionString)
    {
        _connection = new Npgsql.NpgsqlConnection(connectionString);
    }
    public PostgresConfigurationRepository()
    {

    }
    public IEnumerable<AppConfiguration> GetActiveConfigurations()
    {
        _connection ??= new Npgsql.NpgsqlConnection(ConnectionString);

        const string sql = @"
            SELECT id, ""key"", value, label, content_type, 
                   valid_from, expires_at, created_at, modified_at, etag 
            FROM app_configuration 
            WHERE CURRENT_TIMESTAMP BETWEEN valid_from AND expires_at
               OR (valid_from IS NULL AND expires_at IS NULL)";


        return _connection.QuerySql<AppConfiguration>(sql);

    }
}