package jumapacelaya.gob.mx.cobranza.servicio;

import jakarta.inject.Singleton;
import jumapacelaya.gob.mx.cobranza.dto.detalleCarteraDTO;

import javax.sql.DataSource;

import io.micronaut.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Singleton
public class CobranzaServicio {

    private final DataSource dataSource;

    public CobranzaServicio(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Transactional
    public int obtenerTotalPredios() {
        String query = "SELECT COUNT(*) AS TotalPredios FROM cobranza.tblcarteravencida";
        try (Connection connection = dataSource.getConnection();
            PreparedStatement stmt = connection.prepareStatement(query);
            ResultSet rs = stmt.executeQuery()) {

            if (rs.next()) {
                return rs.getInt("TotalPredios");
            } else {
                return 0;
            }

        } catch (Exception e) {
            throw new RuntimeException("Error al obtener total de predios en cartera vencida" +
            e.getMessage(), e);
        }
    }

    @Transactional
    public List<detalleCarteraDTO> obtenerDetalleCartera(int offset, int limit) {
        String query = "SELECT * FROM cobranza.tblcarteravencida OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
        List<detalleCarteraDTO> lista = new ArrayList<>();

        try (Connection connection = dataSource.getConnection();
            PreparedStatement stmt = connection.prepareStatement(query)) {

            stmt.setInt(1, offset);
            stmt.setInt(2, limit);

            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                detalleCarteraDTO dto = new detalleCarteraDTO();
                dto.setPredio(rs.getLong("PREDIO"));
                dto.setNomcliente(rs.getString("NOMCLIENTE"));
                dto.setDireccion(rs.getString("DIRECCION"));
                dto.setColonia(rs.getString("COLONIA"));
                dto.setZona(rs.getString("ZONA"));
                dto.setUso(rs.getString("USO"));
                dto.setTarifa(rs.getString("TARIFA"));
                dto.setActividad(rs.getString("ACTIVIDAD"));
                dto.setHabitado(rs.getString("HABITADO"));
                dto.setVip(rs.getInt("VIP"));
                dto.setMetsuministro(rs.getString("METSUMINISTRO"));
                dto.setMedidor(rs.getString("MEDIDOR"));
                dto.setEstado(rs.getString("ESTADO"));
                dto.setEstadogestion(rs.getString("ESTADOGESTION"));
                dto.setEstadocobranza(rs.getString("ESTADOCOBRANZA"));
                dto.setEstadocorte(rs.getString("ESTADOCORTE"));
                dto.setCortefectivo(rs.getString("CORTEEFECTIVO"));
                dto.setEstadoconvfc(rs.getString("ESTADOCONVFC"));
                dto.setEstadoconvfv(rs.getString("ESTADOCONVFV"));
                dto.setFactvencidas(rs.getInt("FACTVENCIDAS"));
                dto.setAntiguedad(rs.getInt("ANTIGUEDAD"));
                dto.setSaldoiva(rs.getBigDecimal("SALDOIVA"));
                dto.setSaldototal(rs.getBigDecimal("SALDOTOTAL"));
                dto.setR7(rs.getInt("R7"));
                dto.setA7(rs.getInt("A7"));
                dto.setR8(rs.getInt("R8"));
                dto.setA8(rs.getInt("A8"));
                dto.setR11(rs.getInt("R11"));
                dto.setA11(rs.getInt("A11"));
                dto.setR12(rs.getInt("R12"));
                dto.setA12(rs.getInt("A12"));
                dto.setR13(rs.getInt("R13"));
                dto.setA13(rs.getInt("A13"));
                dto.setR14(rs.getInt("R14"));
                dto.setA14(rs.getInt("A14"));
                dto.setR15(rs.getInt("R15"));
                dto.setA15(rs.getInt("A15"));
                dto.setR16(rs.getInt("R16"));
                dto.setA16(rs.getInt("A16"));
                dto.setR17(rs.getInt("R17"));
                dto.setA17(rs.getInt("A17"));
                dto.setR112(rs.getInt("R112"));
                dto.setA112(rs.getInt("A112"));
                dto.setR116(rs.getInt("R116"));
                dto.setA116(rs.getInt("A116"));
                dto.setReconexion(rs.getInt("RECONEXION"));
                dto.setFactventa(rs.getInt("FACTVENTA"));
                dto.setConceptosfv(rs.getString("CONCEPTOSFV"));
                dto.setAdeudofv(rs.getBigDecimal("ADEUDOFV"));
                dto.setPrograma(rs.getString("PROGRAMA"));
                dto.setFid(rs.getString("FID"));

                lista.add(dto);
            }

        } catch (Exception e) {
            throw new RuntimeException("Error al obtener detalle de cartera vencida", e);
        }

        return lista;
    }

    @Transactional
    public List<detalleCarteraDTO> obtenerDetallePorPredio(Long predioId) {
        String query = "SELECT * FROM cobranza.tblcarteravencida WHERE predio = ?";
        List<detalleCarteraDTO> lista = new ArrayList<>();

        try (Connection connection = dataSource.getConnection();
            PreparedStatement stmt = connection.prepareStatement(query)) {

            stmt.setLong(1, predioId);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                detalleCarteraDTO dto = new detalleCarteraDTO();
                dto.setPredio(rs.getLong("PREDIO"));
                dto.setNomcliente(rs.getString("NOMCLIENTE"));
                dto.setDireccion(rs.getString("DIRECCION"));
                dto.setColonia(rs.getString("COLONIA"));
                dto.setZona(rs.getString("ZONA"));
                dto.setUso(rs.getString("USO"));
                dto.setTarifa(rs.getString("TARIFA"));
                dto.setActividad(rs.getString("ACTIVIDAD"));
                dto.setHabitado(rs.getString("HABITADO"));
                dto.setVip(rs.getInt("VIP"));
                dto.setMetsuministro(rs.getString("METSUMINISTRO"));
                dto.setMedidor(rs.getString("MEDIDOR"));
                dto.setEstado(rs.getString("ESTADO"));
                dto.setEstadogestion(rs.getString("ESTADOGESTION"));
                dto.setEstadocobranza(rs.getString("ESTADOCOBRANZA"));
                dto.setEstadocorte(rs.getString("ESTADOCORTE"));
                dto.setCortefectivo(rs.getString("CORTEEFECTIVO"));
                dto.setEstadoconvfc(rs.getString("ESTADOCONVFC"));
                dto.setEstadoconvfv(rs.getString("ESTADOCONVFV"));
                dto.setFactvencidas(rs.getInt("FACTVENCIDAS"));
                dto.setAntiguedad(rs.getInt("ANTIGUEDAD"));
                dto.setSaldoiva(rs.getBigDecimal("SALDOIVA"));
                dto.setSaldototal(rs.getBigDecimal("SALDOTOTAL"));
                dto.setR7(rs.getInt("R7")); dto.setA7(rs.getInt("A7"));
                dto.setR8(rs.getInt("R8")); dto.setA8(rs.getInt("A8"));
                dto.setR11(rs.getInt("R11")); dto.setA11(rs.getInt("A11"));
                dto.setR12(rs.getInt("R12")); dto.setA12(rs.getInt("A12"));
                dto.setR13(rs.getInt("R13")); dto.setA13(rs.getInt("A13"));
                dto.setR14(rs.getInt("R14")); dto.setA14(rs.getInt("A14"));
                dto.setR15(rs.getInt("R15")); dto.setA15(rs.getInt("A15"));
                dto.setR16(rs.getInt("R16")); dto.setA16(rs.getInt("A16"));
                dto.setR17(rs.getInt("R17")); dto.setA17(rs.getInt("A17"));
                dto.setR112(rs.getInt("R112")); dto.setA112(rs.getInt("A112"));
                dto.setR116(rs.getInt("R116")); dto.setA116(rs.getInt("A116"));
                dto.setReconexion(rs.getInt("RECONEXION"));
                dto.setFactventa(rs.getInt("FACTVENTA"));
                dto.setConceptosfv(rs.getString("CONCEPTOSFV"));
                dto.setAdeudofv(rs.getBigDecimal("ADEUDOFV"));
                dto.setPrograma(rs.getString("PROGRAMA"));
                dto.setFid(rs.getString("FID"));

                lista.add(dto);
            }

        } catch (Exception e) {
            throw new RuntimeException("Error al obtener detalle por predio", e);
        }

        return lista;
    }

}
