<?php

declare(strict_types = 1);

namespace MyApp\Model;

use MyApp\Entity\Image_chambre;
use MyApp\Entity\Type_chambre;
use MyApp\Entity\Chambre;
use PDO;

class Image_Chambre_Model{
    private PDO $db;

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

    // on récup toutes les enregistrements de la BDD
    public function getAllImageChambre():array{
        $sql = "SELECT Image_chambre.*, Chambre.*, Type_chambre.* FROM Image_chambre JOIN Chambre ON Chambre.Id_chambre = Image_chambre.Id_chambre JOIN Type_chambre ON Type_chambre.Id_type_chambre = Chambre.Id_type_chambre";
        $stmt = $this->db->query($sql); // contient l'ensemble des résultats
        $image_chambre=[];
        // on organise sous forme d'un tableau les valeurs récupérées
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $chambre = null; 
            if ($row['Id_chambre'] !== null) {

                $typeChambre = null; 
                if ($row['Id_type_chambre'] !== null) {
                    $typeChambre = new Type_chambre($row['Id_type_chambre'], $row['Nom_type']);
                }
                $chambre = new Chambre($row['Id_chambre'], $row['Nom'], floatval($row['Prix']), $row['Nbr_lit'], $row['Num_chambre'], $row['Description'], floatval($row['Superficie']), $row['Etat'], $typeChambre);
            }
            $image_chambre[] = new Image_chambre($row['Id_image_chambre'], $row['Lien_image'], $chambre);
        }

        return $image_chambre;
    }
    // méthode pour récupérer un enrergistrement de la table image_chambre
    public function getOneImageChambre(int $id):?Image_chambre{
        $sql = "SELECT Image_chambre.*, Chambre.*, Type_chambre.* FROM Image_chambre JOIN Chambre ON Chambre.Id_chambre = Image_chambre.Id_chambre JOIN Type_chambre ON Type_chambre.Id_type_chambre = Chambre.Id_type_chambre where Id_image_chambre = :Id_image_chambre";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":Id_image_chambre", $id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if(!$row){
            return null;
        }
        $chambre = null; 
        if ($row['Id_chambre'] !== null) {

            $typeChambre = null; 
            if ($row['Id_type_chambre'] !== null) {
                $typeChambre = new Type_chambre($row['Id_type_chambre'], $row['Nom_type']);
            }
            $chambre = new Chambre($row['Id_chambre'], $row['Nom'], floatval($row['Prix']), $row['Nbr_lit'], $row['Num_chambre'], $row['Description'], floatval($row['Superficie']), $row['Etat'], $typeChambre);
        }
        return new Image_chambre($row['Id_image_chambre'], $row['Lien_image'], $chambre);
    }

    // méthode pour ajoutet un enregistrement à la table
    public function createImageChambre(Image_chambre $image_chambre): bool {
        $sql = "INSERT INTO Image_chambre (Id_image_chambre, Lien_image, Id_chambre) VALUES (:Id_image_chambre, :Lien_image, :Id_chambre)";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':Id_image_chambre', $image_chambre->getId(), PDO::PARAM_STR);
        $stmt->bindValue(':Lien_image', $image_chambre->getLien_image(), PDO::PARAM_STR);
        $stmt->bindValue(':Id_chambre', $image_chambre->getId_chambre()->getId(), PDO::PARAM_STR);
        return $stmt->execute();
    }

    
    // suppprimer un enregistrement d'une image de chambre
    public function deleteImageChambre(int $id): bool {
        $sql = "DELETE FROM Image_chambre WHERE Id_image_chambre = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
        return $stmt->execute();
    }

    // modifier l'enregistrement d'une image d'une chambre existente
    public function updateImageChambre(Image_chambre $image_chambre): bool {
        $sql = "UPDATE Image_chambre 
        SET Id_image_chambre = :Id_image_chambre, 
            Lien_image = :Lien_image, 
            Id_chambre = :Id_chambre 
        WHERE Id_image_chambre = :Id_image_chambre";
        
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':Id_image_chambre', $image_chambre->getId(), PDO::PARAM_STR);
        $stmt->bindValue(':Lien_image', $image_chambre->getLien_image(), PDO::PARAM_STR);
        $stmt->bindValue(':Id_chambre', $image_chambre->getId_chambre()->getId(), PDO::PARAM_STR);
        return $stmt->execute();
    }
        

}
