<?php
namespace MyApp\Service;

use PDO;
use MyApp\Model\image_Chambre_Model;
use MyApp\Model\type_Chambre_Model;
use MyApp\Model\chambre_Model;
use MyApp\Model\compte_Model;
use MyApp\Model\role_Model;
use MyApp\Model\etablissement_Model;
use MyApp\Model\facture_Model;
use MyApp\Model\proposer_Model;
use MyApp\Model\reservation_Model;
use MyApp\Model\services_Disponible_Model;

class DependencyContainer
{
    private $instances = [];

    public function __construct()
    {
    }

    public function get($key)
    {
        if (!isset($this->instances[$key])) {
            $this->instances[$key] = $this->createInstance($key);
        }

        return $this->instances[$key];
    }

    private function createInstance($key)
    {
        switch ($key) {

            case 'PDO': return $this->createPDOInstance();

            case 'Image_Chambre_Model' :
                $pdo = $this->get('PDO');
                return new Image_Chambre_Model($pdo);

            case 'Type_Chambre_Model' :
                $pdo = $this->get('PDO');
                return new Type_Chambre_Model($pdo);
            
            case 'Chambre_Model' :
                $pdo = $this->get('PDO');
                return new Chambre_Model($pdo);

            case 'Compte_Model' :
                $pdo = $this->get('PDO');
                return new Compte_Model($pdo);
            
            case 'Role_Model' :
                $pdo = $this->get('PDO');
                return new Role_Model($pdo);

            case 'Etablissement_Model' :
                $pdo = $this->get('PDO');
                return new Etablissement_Model($pdo);

            case 'Facture_Model' :
                $pdo = $this->get('PDO');
                return new Facture_Model($pdo);

            case 'Proposer_Model' :
                $pdo = $this->get('PDO');
                return new Proposer_Model($pdo);

            case 'Reservation_Model' :
                $pdo = $this->get('PDO');
                return new Reservation_Model($pdo);

            case 'Services_Disponible_Model' :
                $pdo = $this->get('PDO');
                return new Services_Disponible_Model($pdo);

            default:
                throw new \Exception("No service found for key: " . $key);
        }
    }

    private function createPDOInstance(){
        try{
            $pdo = new PDO('mysql:host='.$_ENV['DB_HOST']. ';dbname='.$_ENV['DB_NAME'].';charset=utf8', $_ENV['DB_USER'],$_ENV['DB_PASS']);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $pdo;
        }
        catch(PDOException $e){
            throw new \Exception("PDO connection error".$e->getMessages);
        }
    }

}
?>
