package controlador;

import modelo.*;
import vista.VentaBoletosView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class VentaBoletosController {
    private VentaBoletosView vista;
    private Database database;
    private List<JButton> botonesAsientos;

    public VentaBoletosController(VentaBoletosView vista, Database database) {
        this.vista = vista;
        this.database = database;
        this.botonesAsientos = new ArrayList<>();

        cargarAsientos();
        agregarListeners();
    }

    private void cargarAsientos() {
        try {
            Connection conexion = database.getConexion();
            Statement stmt = conexion.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM asientos");

            while (rs.next()) {
                int numeroAsiento = rs.getInt("numero_asiento");
                boolean disponible = rs.getBoolean("disponible");

                JButton btnAsiento = new JButton(String.valueOf(numeroAsiento));
                btnAsiento.setEnabled(disponible);
                btnAsiento.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        venderAsiento(numeroAsiento);
                    }
                });

                botonesAsientos.add(btnAsiento);
                vista.agregarBotonAsiento(btnAsiento);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void venderAsiento(int numeroAsiento) {
        try {
            Connection conexion = database.getConexion();
            PreparedStatement pstmt = conexion.prepareStatement("UPDATE asientos SET disponible = FALSE WHERE numero_asiento = ?");
            pstmt.setInt(1, numeroAsiento);
            pstmt.executeUpdate();

            pstmt = conexion.prepareStatement("INSERT INTO ventas (fecha_venta, numero_asiento, precio) VALUES (NOW(), ?, ?)");
            pstmt.setInt(1, numeroAsiento);
            pstmt.setDouble(2, 100.00); // Precio fijo por simplicidad
            pstmt.executeUpdate();

            vista.mostrarMensaje("Boleto vendido: Asiento " + numeroAsiento);
            actualizarBotones();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void actualizarBotones() {
        for (JButton btn : botonesAsientos) {
            btn.setEnabled(false);
        }
        vista.actualizarBotones();
    }

    private void generarReporte() {
        try {
            Connection conexion = database.getConexion();
            Statement stmt = conexion.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM ventas");

            StringBuilder reporte = new StringBuilder("Reporte de Ventas:\n");
            while (rs.next()) {
                reporte.append("Fecha: ").append(rs.getTimestamp("fecha_venta")).append(", ");
                reporte.append("Asiento: ").append(rs.getInt("numero_asiento")).append(", ");
                reporte.append("Precio: $").append(rs.getDouble("precio")).append("\n");
            }

            vista.mostrarMensaje(reporte.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void agregarListeners() {
        vista.getBtnGenerarReporte().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                generarReporte();
            }
        });
    }
}