<?php

namespace Models;

use Bases\Model;

class Plat extends Model
{

    protected $table = "plats";

    /**
     * Retourne les plats de la catégorie entrée et les tags
     *
     * @return array
     */
    public function platsEntrees()
    {
        $sql = " SELECT $this->table.*,
                        GROUP_CONCAT(tags.nom SEPARATOR '/ ') AS tags
                FROM 
                    $this->table
                JOIN 
                    categories ON $this->table.categorie_id = categories.id
                LEFT JOIN
                    plats_tags ON $this->table.id = plats_tags.plat_id
                LEFT JOIN
                    tags ON plats_tags.tag_id = tags.id
                WHERE
                    $this->table.categorie_id = 1
                GROUP BY
                    $this->table.id";

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

        $requete->execute();

        return $requete->fetchAll();
    }

    /**
     * Retourne les plats de la catégorie repas et les tags
     *
     * @return void
     */
    public function platsRepas()
    {
        $sql = " SELECT $this->table.*,
                        GROUP_CONCAT(tags.nom SEPARATOR '/ ') AS tags
                FROM 
                    $this->table
                JOIN 
                    categories ON $this->table.categorie_id = categories.id
                LEFT JOIN
                    plats_tags ON $this->table.id = plats_tags.plat_id
                LEFT JOIN
                    tags ON plats_tags.tag_id = tags.id
                WHERE
                    $this->table.categorie_id = 2
                GROUP BY
                    $this->table.id";

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

        $requete->execute();

        return $requete->fetchAll();
    }

    /**
     * Retourne les plats de la catégorie dessert et les tags
     *
     * @return void
     */
    public function platsDessert()
    {
        $sql = " SELECT $this->table.*,
                        GROUP_CONCAT(tags.nom SEPARATOR '/ ') AS tags
                FROM 
                    $this->table
                JOIN 
                    categories ON $this->table.categorie_id = categories.id
                LEFT JOIN
                    plats_tags ON $this->table.id = plats_tags.plat_id
                LEFT JOIN
                    tags ON plats_tags.tag_id = tags.id
                WHERE
                    $this->table.categorie_id = 3
                GROUP BY
                    $this->table.id";

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

        $requete->execute();

        return $requete->fetchAll();
    }

    /**
     * Retourne un plat en fonction de son id
     *
     * @param int $id
     * @return object|false
     */
    public function platParId($id)
    {

        $sql = "SELECT $this->table.*
                FROM $this->table
                WHERE $this->table.id = :id";

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

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

        return $requete->fetch();
    }

    /**
     * inserer un nouveau plat
     *
     * @param string $nom
     * @param string $description
     * @param string $prix
     * @param string $media
     * @param int $categorie_id
     * 
     * @return bool
     */
    public function inserer($nom, $description, $prix, $media, $categorie_id)
    {

        // Insérer le plat dans la table plats
        $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" => $media,
            ":categorie_id" => $categorie_id
        ]);
    }

    public function insererTags($tags)
    {
        $plat_id = $this->pdo()->lastInsertId();

        $sql = "INSERT INTO plats_tags 
                        (plat_id, tag_id) 
                    VALUES 
                        (:plat_id, :tag_id)";

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

        foreach ($tags as $tag) {

            $success = $requete->execute([
                ":plat_id" => $plat_id,
                ":tag_id" => $tag
            ]);
        }

        if (!$success) {
            return false;
        }
        return true;
    }

    /**
     * Modifier un plat
     *
     * @param int $id
     * @param string $nom
     * @param string $description
     * @param string $prix
     * @param string $media
     * @param string $categorie_id
     * 
     * @return bool
     */
    public function modifier($id, $nom, $description, $prix, $media, $categorie_id)
    {
        $sql = "UPDATE $this->table 
                SET nom = :nom,
                    description = :description,
                    prix = :prix,
                    image = :image,
                    categorie_id = :categorie_id
                WHERE $this->table.id = $id";

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

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

    /**
     * Modifier les tags du plat selectionner
     *
     * @param int $id
     * @param array $tags
     * 
     * @return bool
     */
    public function modifierTags($id, $tags)
    {

        foreach ($tags as $tag) {
            $sql = "INSERT INTO plats_tags 
                            (plat_id, tag_id) 
                        VALUES 
                            (:plat_id, :tag_id)";

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

            foreach ($tags as $tag) {

                $success = $requete->execute([
                    ":plat_id" => $id,
                    ":tag_id" => $tag

                ]);
            }

            if (!$success) {
                return false;
            }
            return true;
        }
    }

    /**
     * Supprime les tags d'un plat
     *
     * @param int $id
     * 
     */
    public function supprimerTags($id)
    {

        $sql = "DELETE FROM plats_tags
                WHERE plat_id = :plat_id";

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

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

    /**
     * Supprime un plat
     *
     * @param int $id
     * @return bool
     */
    function supprimer($id)
    {

        $sql = "DELETE FROM $this->table 
                WHERE id = :id";

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

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