<?php

namespace Gateway;
use PDO;

class ImageGateway extends Gateway
{

    public function createImgGateway(int $idImg, string $imgPath) : bool
    {
        $query = "
            INSERT INTO Image
            VALUES (:id_img, :img_path)
        ";

        return $this -> co -> executeQuery($query, [
            'id_img' => array($idImg, PDO::PARAM_INT),
            'img_path' => array($imgPath, PDO::PARAM_STR),
        ]);
    }

    public function findImgById(int $idImg) : array
    {
        $query = "
            SELECT * FROM Image
            WHERE id_img = :id_img
        ";

        $this -> co -> executeQuery($query, ['id_img' => array($idImg, PDO::PARAM_INT)]);

        return $this -> co -> getResults();
    }

    public function findAllImg() : array
    {
        $query = "
            SELECT * FROM Image
        ";

        $this -> co -> executeQuery($query);

        return $this -> co -> getResults();
    }

    public function deleteImgGateway(int $idImg) : bool
    {
        $query = "
            DELETE FROM Image
            WHERE id_img = :id_img
        ";

        return $this -> co -> executeQuery($query, ['id_img' => array($idImg, PDO::PARAM_INT)]);
    }

    public function updateImgGateway(int $idImg, string $imgPath) : bool
    {
        $query = "
            UPDATE Image
            SET imgPath = :img_path
            WHERE id_image = :id_img
        ";

        return $this -> co -> executeQuery($query, [
            'id_img' => array($idImg, PDO::PARAM_INT),
            'img_path' => array($imgPath, PDO::PARAM_STR)
        ]);
    }

    public function getLastId():int{
        $query = "SELECT id_img FROM Image;";
        $this -> co -> executeQuery($query);
        $res = $this -> co -> getResults();
        foreach($res as $r){
            $tab[] = $r["id_img"];
        }
        $id = 1;
        while(in_array($id,$tab))
        {
            $id=$id+1;
        }
        return $id;
    }
}