<?php

namespace Models;

use Bases\Model;

class Menu extends Model
{
    protected $table = "menus";

    /**
     * Retourne toutes les entrées du menu
     */
    public function toutMenuEntree()
    {
        $sql = "SELECT 
                    $this->table.*,
                    categories.nom AS nom_categorie,
                    tags.nom AS nom_tag
                FROM
                    $this->table
                JOIN categories
                ON $this->table.categorie_id = categories.id
                JOIN menu_tag
                ON $this->table.id = menu_tag.menu_id
                JOIN tags
                ON menu_tag.tag_id = tags.id
                WHERE
                    categories.nom = 'Entrée'
                GROUP BY $this->table.id";

        $requete = $this->pdo()->prepare($sql);

        $requete->execute();

        return $requete->fetchAll();
    }

    /**
     * Retourne tous les repas du menu
     */
    public function toutMenuRepas()
    {
        $sql = "SELECT 
                    $this->table.*,
                    categories.nom AS nom_categorie,
                    tags.nom AS nom_tag
                FROM
                    $this->table
                JOIN categories
                ON $this->table.categorie_id = categories.id
                JOIN menu_tag
                ON $this->table.id = menu_tag.menu_id
                JOIN tags
                ON menu_tag.tag_id = tags.id
                WHERE
                    categories.nom = 'Repas'
                GROUP BY $this->table.id";

        $requete = $this->pdo()->prepare($sql);

        $requete->execute();

        return $requete->fetchAll();
    }

    /**
     * Retourne tous les repas du menu
     */
    public function toutMenuDessert()
    {
        $sql = "SELECT 
                    $this->table.*,
                    categories.nom AS nom_categorie,
                    tags.nom AS nom_tag
                FROM
                    $this->table
                JOIN categories
                ON $this->table.categorie_id = categories.id
                JOIN menu_tag
                ON $this->table.id = menu_tag.menu_id
                JOIN tags
                ON menu_tag.tag_id = tags.id
                WHERE
                    categories.nom = 'Dessert'
                GROUP BY $this->table.id";

        $requete = $this->pdo()->prepare($sql);

        $requete->execute();

        return $requete->fetchAll();
    }

    /**
     * Récupérer un plat selon un id
     *
     * @param int $id
     * @return object|false
     */
    public function menuParId($id){
        $sql = "SELECT 
                    $this->table.*, menu_tag.tag_id AS tag_id
                FROM 
                    $this->table 
                JOIN 
                    menu_tag
                ON
                    $this->table.id = menu_tag.menu_id
                where $this->table.id = :id";

        $requete = $this->pdo()->prepare($sql);

        $requete->execute([
            ":id" => $id
        ]);

        return $requete->fetch();
    }

    /**
     * Modifie les données d'un plat
     *
     * @param string $nom
     * @param string $description
     * @param float $prix
     * @param string $image
     * @param int $categorie_id
     * @return bool
     */
    public function modifierMenu($id, $nom, $description, $prix, $image, $categorie_id){

        /* Normalement, il faudrait que je modifie également les tags dans menu_tag grace au tableau $_POST["tags"] mais tout marche sauf ça et je ne souhaite pas risqué de brisé le tout*/        
        $sql = "UPDATE 
                    $this->table
                SET 
                    nom = :nom, 
                    description = :description, 
                    prix = :prix,
                    image = :image,
                    categorie_id = :categorie_id
                WHERE id = :id";

        $requete = $this->pdo()->prepare($sql);

        $requete->execute([
            ":id" =>$id,
            ":nom" =>$nom,
            ":description" =>$description,
            ":prix" =>$prix,
            ":image" =>$image,
            ":categorie_id" =>$categorie_id
        ]);
        
        return $requete;


    }

    /**
     * Insère un nouveau plat dans la table menu
     *
     * @param string $nom
     * @param string $description
     * @param float $prix
     * @param string $image
     * @param int $categorie_id
     * @return bool
     */
    public function insererMenu($nom, $description, $prix, $image, $categorie_id)
    {
        $sql = "INSERT INTO $this->table
                (nom, description, prix, image, categorie_id)
                VALUES (:nom, :description, :prix, :image, :categorie_id)";

        
        $requete = $this->pdo()->prepare($sql);

        return $requete->execute([
            ":nom" => $nom,
            ":description" => $description,
            ":prix" => $prix,
            ":image" => $image,
            ":categorie_id" => $categorie_id
        ]);
        
    }

    /**
     * Insère le nouveau plat dans la table menu_tag afin de faire le lien entre le plat et les tags
     *
     * @param int $tag_id
     * @return bool
     */
    public function insererMenuTag($tag_id){
        $sql = "INSERT INTO menu_tag 
                (menu_id, tag_id) 
                VALUES (:menu_id, :tag_id)";

        $requete = $this->pdo()->prepare($sql);

        // Récupérer le dernier ID inséreé
        $dernier_id_menu = $this->pdo()->lastInsertID();

        return $requete->execute([
            ":menu_id" => $dernier_id_menu,
            ":tag_id" => $tag_id
        ]);
    }

    /**
     * Supprimer un plat du menu (menu_tag)
     *
     * @param int $id
     * @return bool
     */
    public function supprimerMenuTag($id)
    {
        // Supression du menu dans la table pivot entre menu et tags
        $sql = "DELETE FROM menu_tag WHERE menu_tag.menu_id = :id";

        $requete= $this->pdo()->prepare($sql);

        return $requete->execute([
            ":id" => $id
        ]);
    }
    /**
     * Supprimer un plat du menu (menus)
     *
     * @param int $id
     * @return bool
     */
    public function supprimerMenu($id)
    {
        // Supression du menu 
        $sql= "DELETE FROM $this->table WHERE $this->table.id = :id";

        $requete= $this->pdo()->prepare($sql);

        return $requete->execute([
            ":id" => $id
        ]);
    }
}
