<?php

declare(strict_types = 1);

namespace MyApp\Model;

use MyApp\Entity\Etablissement;
use PDO;

class Etablissement_Model{
    private PDO $db;

    public function __construct(PDO $db){
        $this->db = $db;
    }

    // on récup toutes les enregistrements de la BDD
    public function getAllEtablissements():array{
        $sql = "SELECT * FROM Etablissement";
        $stmt = $this->db->query($sql); // contient l'ensemble des résultats
        $etablissements=[];
        // on organise sous forme d'un tableau les valeurs récupérées
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $etablissements[] = new Etablissement($row['Id_etablissement'], $row['Num_telephone'], $row['Mail'], $row['Adresse'], $row['RIB']);
        }

        return $etablissements;
    }

    public function getOneEtablissements(int $id):?Etablissement{
        $sql = "SELECT * from Etablissements where id = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if(!$row){
            return null;
        }
        return new Etablissement($row['Id_etablissement'], $row['Num_telephone'], $row['Mail'], $row['Adresse'], $row['RIB']);
    }


}
