<?php
    include_once __DIR__ . '/../config/database.php';

    class AdminController {
        private $db;

        public function __construct() {
            $database = new Database();
            $this->db = $database->getConnection();
        }

        public function listarAdministradores() {
            $query = "SELECT * FROM user";
            $stmt = $this->db->prepare($query);
            $stmt->execute();
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

        public function agregarAdministrador($username, $password, $name, $emailid, $sucursal, $idRol) {
            $query = "INSERT INTO user (username, password, name, emailid, sucursal_varchar, idRol) VALUES (:username, :password, :name, :emailid, :sucursal, :idRol)";
            $stmt = $this->db->prepare($query);
            $passwordHasheada = password_hash($password, PASSWORD_BCRYPT);
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':password', $passwordHasheada);
            $stmt->bindParam(':name', $name);
            $stmt->bindParam(':emailid', $emailid);
            $stmt->bindParam(':sucursal', $sucursal);
            $stmt->bindParam(':idRol', $idRol);
            return $stmt->execute();
        }

        public function actualizarAdministrador($id, $username, $name, $emailid, $sucursal, $idRol) {
            $query = "UPDATE user SET username = :username, name = :name, emailid = :emailid, sucursal_varchar = :sucursal, idRol = :idRol WHERE id = :id";
            $stmt = $this->db->prepare($query);
            $stmt->bindParam(':id', $id);
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':name', $name);
            $stmt->bindParam(':emailid', $emailid);
            $stmt->bindParam(':sucursal', $sucursal);
            $stmt->bindParam(':idRol', $idRol);
            return $stmt->execute();
        }

        public function eliminarAdministrador($id) {
            $query = "DELETE FROM user WHERE id = :id";
            $stmt = $this->db->prepare($query);
            $stmt->bindParam(':id', $id);
            return $stmt->execute();
        }
    }
?>
