/*
 * 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.Utilisateur;

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

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

    public Utilisateur readAuthentif(String login, String mdp) {
        try {
            PreparedStatement prepStmt = null;
            String sql = "SELECT * FROM `utilisateur` where login = ? AND mdp = MD5(?);";
            prepStmt = theConnection.prepareStatement(sql);
            prepStmt.setString(1, login);
            prepStmt.setString(2, mdp);
            ResultSet result = prepStmt.executeQuery();
            if (result.next()) {
                return new Utilisateur(result.getString(1), result.getString(2),
                        result.getString(3), result.getString(4), null, result.getInt(0));
            }
            prepStmt.close();
            return null;
        } catch (SQLException ex) {
            System.out.println("SQLExeption : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
            return null;
        }
    }

    public int createUser(String firstname, String lastname, String mdp, String fonction) {
        try {
            String query = "INSERT INTO `salarie` (nom, prenom, fonction) VALUES (?, ?, ?)";
            PreparedStatement stmt = theConnection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1, firstname);
            stmt.setString(2, lastname);
            stmt.setString(3, fonction);
            // stmt.setString(3, mdp);
            // stmt.setString(4, firstname + lastname + "@gmail.com");
            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;
        }
    }

    public List<Utilisateur> readAll() {
        try {
            List<Utilisateur> users = new ArrayList<>();
            String query = "SELECT prenom, nom, fonction, id FROM salarie";
            Statement stmt = theConnection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {
                String nom = rs.getString("nom");
                String prenom = rs.getString("prenom");
                String fonction = rs.getString("fonction");
                int id = rs.getInt("id");

                Utilisateur user = new Utilisateur(nom, prenom, null, null, fonction, id);
                users.add(user);
            }
            return users;
        } 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<>();
        }
    }
}
