<?php

declare(strict_types=1);

namespace MyApp\Model;

use MyApp\Entity\Reservation;
use MyApp\Entity\Facture;
use MyApp\Entity\Chambre;
use MyApp\Entity\Type_Chambre;
use MyApp\Entity\Compte;
use MyApp\Entity\Etablissement;
use MyApp\Entity\Role;
use PDO;

class Reservation_Model
{
    private PDO $db;

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

    // Retrieve all records from the database
    public function getAllReservation(): array
    {
        $sql = "SELECT Reservation.*, Facture.*, Etablissement.*, Chambre.*, Type_chambre.*, Compte.*, Role.* FROM Reservation
                JOIN Facture ON Facture.Id_Facture = Reservation.Id_facture
                JOIN Etablissement ON Etablissement.Id_etablissement = Facture.Id_etablissement
                JOIN Chambre ON Chambre.Id_chambre = Reservation.Id_chambre
                JOIN Type_chambre ON Type_chambre.Id_type_chambre = Chambre.Id_type_chambre
                JOIN Compte ON Compte.Id_compte = Reservation.Id_compte
                JOIN Role ON Role.Id_role = Compte.Id_role";

        $stmt = $this->db->query($sql);
        $reservations = [];

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $facture = null;
            if ($row['Id_facture'] !== null) {
                $etablissement = null;
                if ($row['Id_etablissement'] !== null) {
                    $etablissement = new Etablissement($row['Id_etablissement'], $row['Num_telephone'], $row['Mail'], $row['Adresse'], $row['RIB']);
                }
                $facture = new Facture($row['Id_facture'], floatval($row['Prix_total']), floatval($row['Prix_HT']), $row['Adresse_facturation'], $row['Mobilites_paiement'], $row['Date_facturation'], $row['Details_facturation'], $etablissement);
            }

            $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'], $type_chambre);
            }

            $compte = null;
            if ($row['Id_compte'] !== null) {
                $role = null;
                if ($row['Id_role'] !== null) {
                    $role = new Role($row['Id_role'], $row['Nom_role']);
                }
                $compte = new Compte($row['Id_compte'], $row['Nom'], $row['Prenom'], $row['Num_telephone'], $row['Mail'], $row['Mot_de_passe'], $role);
            }

            $reservations[] = new Reservation($row['Id_reservation'], $row['Date_reception'], $row['Date_remise'], $facture, $chambre, $compte);
        }

        return $reservations;
    }

    // Retrieve a single record from the reservation table
    public function getOneReservation(int $id): ?Reservation
    {
        $sql = "SELECT * FROM Reservation WHERE id = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$row) {
            return null;
        }

        $facture = null;
        if ($row['Id_facture'] !== null) {
            $etablissement = null;
            if ($row['Id_etablissement'] !== null) {
                $etablissement = new Etablissement($row['Id_etablissement'], $row['Num_telephone'], $row['Mail'], $row['Adresse'], $row['RIB']);
            }
            $facture = new Facture($row['Id_facture'], floatval($row['Prix_total']), floatval($row['Prix_HT']), $row['Adresse_facturation'], $row['Mobilites_paiement'], $row['Date_facturation'], $row['Details_facturation'], $etablissement);
        }

        $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'], $type_chambre);
        }

        $compte = null;
        if ($row['Id_compte'] !== null) {
            $role = null;
            if ($row['Id_role'] !== null) {
                $role = new Role($row['Id_role'], $row['Nom_role']);
            }
            $compte = new Compte($row['Id_compte'], $row['Nom'], $row['Prenom'], $row['Num_telephone'], $row['Mail'], $row['Mot_de_passe'], $role);
        }

        return new Reservation($row['Id_reservation'], $row['Date_reception'], $row['Date_remise'], $facture, $chambre, $compte);
    }

    public function createReservation(Reservation $reservation): bool{
        $sql = "INSERT INTO Reservation (Id_reservation, Date_reception, Date_remise, Id_facture, Id_chambre, Id_compte) 
                VALUES (:Id_reservation, :Date_reception, :Date_remise, :Id_facture, :Id_chambre, :Id_compte)";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':Id_reservation', $reservation->getId(), PDO::PARAM_INT);
        $stmt->bindValue(':Date_reception', $reservation->getDate_reception(), PDO::PARAM_STR);
        $stmt->bindValue(':Date_remise', $reservation->getDate_remise(), PDO::PARAM_STR);
        
        // Assuming getId_facture(), getId_chambre(), and getId_compte() return integer IDs
        $stmt->bindValue(':Id_facture', $reservation->getId_facture(), PDO::PARAM_INT);
        $stmt->bindValue(':Id_chambre', $reservation->getId_chambre(), PDO::PARAM_INT);
        $stmt->bindValue(':Id_compte', $reservation->getId_compte(), PDO::PARAM_INT);
    
        return $stmt->execute();
    }
    
}