using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Models;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;

namespace DAL.Implementacion
{
    public class DGAMedicionScadaRepository
    {
        private readonly IConfiguration _config;

        public DGAMedicionScadaRepository(IConfiguration config)
        {
            _config = config;
        }

        public async Task<List<MedicionScada>> ObtenerMedicionesAsync()
        {
            List<MedicionScada> listaMedicion = new List<MedicionScada>();
            string conexion = _config.GetConnectionString("DefaultConnection");

            using (SqlConnection connection = new SqlConnection(conexion))
            {
                await connection.OpenAsync();
                string query = "Select code, dateOrigen,dateMedicionSup,caudal,altura,dateMedicionSub,totalizador,caudalSub,nivel from Medicion_DGA_SMARTSCADA";

                using(var command = new SqlCommand(query, connection))
                {
                    using (var reader = await command.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            MedicionScada medicion = new MedicionScada
                            {
                                Code = reader["code"] as string,
                                DateOrigen = reader["dateOrigen"] as DateTime?,
                                DateMedicionSup = reader["dateMedicionSup"] as DateTime?,
                                Caudal = reader["caudal"] as decimal?,
                                Altura = reader["altura"] as decimal?,
                                DateMedicionSub = reader["dateMedicionSub"] as DateTime?,
                                Totalizador = reader["totalizador"] as decimal?,
                                Caudalsub = reader["caudalSub"] as decimal?,
                                Nivel = reader["nivel"] as decimal?
                            };
                            listaMedicion.Add(medicion);
                        }
                    }
                }
            }
            Console.WriteLine($"Se han obtenido {listaMedicion.Count} mediciones de la DGA SMARTSCADA.\n");
           
            return listaMedicion;
        }

    }
}
