<?php

declare (strict_types = 1);

namespace MyApp\Model;

use MyApp\Entity\Chambre;
use MyApp\Entity\Proposer;
use MyApp\Entity\Services_disponible;
use MyApp\Entity\Type_chambre;
use PDO;

class Proposer_Model
{
    private PDO $db;

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

    // on récup toutes les enregistrements de la BDD
    public function getAllProposer(): array
    {
        $sql = "SELECT Proposer.*, Chambre.*, Type_chambre.*, Services_Disponible.* FROM Proposer JOIN Chambre ON Chambre.Id_chambre = Proposer.Id_chambre JOIN Type_chambre ON Type_chambre.Id_type_chambre = Chambre.Id_type_chambre JOIN Services_Disponible ON Services_Disponible.Id_services_Disponible = Proposer.Id_services_Disponible";
        $stmt = $this->db->query($sql); // contient l'ensemble des résultats
        $proposer = [];
        // 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) {

                $type_chambre = null;
                if ($row['Id_type_chambre'] !== null) {
                    $type_chambre = 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);
            }
            $services_disponible = null;
            if ($row['Id_services_disponible'] !== null) {
                $services_disponible = new Services_disponible($row['Id_services_disponible'], $row['Service_propose']);
            }

            $proposer[] = new Proposer($chambre, $services_disponible);
        }

        return $proposer;
    }

    public function getOneProposer(int $id): ?Proposer
    {
        $sql = "SELECT Proposer.*, Chambre.*, Type_chambre.*, Services_Disponible.* FROM Proposer JOIN Chambre ON Chambre.Id_chambre = Proposer.Id_chambre JOIN Type_chambre ON Type_chambre.Id_type_chambre = Chambre.Id_type_chambre JOIN Services_Disponible ON Services_Disponible.Id_services_disponible = Proposer.Id_services_disponible where id = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$row) {
            return null;
        }
        $chambre = null;
        if ($row['Id_chambre'] !== null) {

            $type_chambre = null;
            if ($row['Id_type_chambre'] !== null) {
                $type_chambre = 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);
        }
        $services_disponible = null;
        if ($row['Id_services_disponible'] !== null) {
            $services_disponible = new Services_disponible($row['Id_services_disponible'], $row['Service_propose']);
        }
        return new Proposer($chambre, $services_disponible);
    }

    public function getServicesForOneChambre(int $id)
    {
        $sql = "SELECT Service_propose
        FROM Services_Disponible
        WHERE Id_services_disponible IN (
            SELECT Id_services_disponible
            FROM Proposer
            WHERE Id_chambre = $id+1
        )";

        $stmt = $this->db->prepare($sql);
        $stmt->execute();
        $services_disponibles = array();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if (!empty($result)) {
            // Stocker chaque service disponible dans le tableau
            foreach ($result as $row) {
                $services_disponibles[] = $row["Service_propose"];
            }
        }

        return $services_disponibles;
    }}
