package examenes.examen20250509.controlador;

import examenes.examen20250509.modelo.Concierto;
import examenes.examen20250509.modelo.Sala;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ControladorSala {
    public static List<Sala> getAllSalas() {
        Connection conn = GestorConexiones.getConnection();
        List<Sala> salas = new ArrayList<>();
        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select * from sala");

            while (rs.next()) {
                salas.add(new Sala(
                        rs.getInt("id"),
                        rs.getString("nombre"),
                        rs.getString("ciudad"),
                        rs.getInt("aforo")
                ));
            }
            return salas;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Sala getSalaById(int id) {
        Connection conn = GestorConexiones.getConnection();
        try {
            PreparedStatement pt = conn.prepareStatement("select * from sala where id = ?");
            pt.setInt(1, id);
            ResultSet rs = pt.executeQuery();

            rs.next();

            return new Sala(
                    rs.getInt("id"),
                    rs.getString("nombre"),
                    rs.getString("ciudad"),
                    rs.getInt("aforo")
            );


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<String> obtenerCiudades() {
        Connection conn = GestorConexiones.getConnection();
        List<String> ciudades = new ArrayList<>();
        try {
            Statement pt = conn.createStatement();
            ResultSet rs = pt.executeQuery("select distinct ciudad from sala");

            while (rs.next()) {
                ciudades.add(rs.getString("ciudad"));
            }
            return ciudades;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
