/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package mysql;

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

import bean.Ventilation;
import util.TrmtDate;

/**
 *
 * @author francois
 */
public class attendanceMysql {
    private Connection theConnection;

    public attendanceMysql() {
        Connexion connexion = new Connexion("127.0.0.1", "bdPersonnel", "java", "java");
        this.theConnection = connexion.getConnection();
    }

    public List<Ventilation> readAll(Date startWeek, Date endWeek) {
        try {
            List<Ventilation> ventils = new ArrayList<>();
            String query = "SELECT * FROM `ventilation` WHERE jourVentil BETWEEN ? AND ?";
            PreparedStatement stmt = theConnection.prepareStatement(query);
            stmt.setDate(1, startWeek);
            stmt.setDate(2, endWeek);
            ResultSet rs = stmt.executeQuery();

           while (rs.next()) {
                Date jourVentil = rs.getDate("jourVentil");
                String periode = rs.getString("periode");
                int idSalarie = rs.getInt("idSalarie");

                Integer ventil = rs.getInt("ventil");

                String nomJour = TrmtDate.getDayOnDate(jourVentil);

                Ventilation ventilData = new Ventilation(jourVentil, idSalarie, periode, ventil, nomJour);

                if (ventilData != null) {
                    ventils.add(ventilData);
                }
            }
            return ventils;
        } catch (SQLException ex) {
            System.out.println("SQLExeption : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
            return new ArrayList<>();
        }
    }

    public static int createVentil(Date jourVentil, String periode, int idSalarie, int ventil) {
        try {
            String query = "INSERT INTO `ventilation` (jourVentil, periode, idSalarie, ventil) VALUES (?, ?, ?, ?)";
            Connexion connexion = new Connexion("127.0.0.1", "bdPersonnel", "java", "java");
            PreparedStatement stmt = connexion.getConnection().prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.setDate(1, jourVentil);
            stmt.setString(2, periode);
            stmt.setInt(3, idSalarie);
            stmt.setInt(4, ventil);
            int affectedRows = stmt.executeUpdate();

            if (affectedRows > 0) {
                ResultSet generatedKeys = stmt.getGeneratedKeys();
                if (generatedKeys.next()) {
                    long newId = generatedKeys.getLong(1);
                    return (int) newId;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } catch (SQLException ex) {
            System.out.println("SQLExeption : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
            return 0;
        }
    }
}
