<?php

namespace Gateway;
use Connection;
use PDO;


class CharacterGateway
{

    private Connection $co;

    public function __construct(Connection $co)
    {
        $this -> co = $co;
    }

    public function create(int $id_character, string $name , string $img_char) : bool
    {
        $query = "
            INSERT INTO Characters
            VALUES(:id_char, :name, :img_char)
        ";

        return $this -> co -> executeQuery($query, [
            'id_char' => array($id_character, PDO::PARAM_INT),
            'name' => array($name, PDO::PARAM_STR),
            'img_char' => array($img_char, PDO::PARAM_STR)
        ]);

    }

    public function findById(int $id) : array
    {
        $query = "SELECT * FROM Characters WHERE id_char = :id_c";
        $this -> co -> executeQuery($query, ["id_c" => array($id, PDO::PARAM_INT)]);
        return $this -> co -> getResults();
    }

    public function findByName(string $name) : array
    {
        $query = "SELECT * FROM Characters WHERE name = :n";
        $this -> co -> executeQuery($query, ["n" => array($name, PDO::PARAM_STR)]);
        return $this -> co -> getResults();
    }

    public function findAll() : array
    {
        $query = "SELECT * FROM Characters";
        $this -> co -> executeQuery($query);
        return $this -> co -> getResults();
    }

    public function delete(int $id) : bool
    {
        $query = "DELETE FROM Characters WHERE id_char = :id_c";
        return $this -> co -> executeQuery($query, ["id_c" => array($id, PDO::PARAM_INT)]);
    }

    public function update(int $id_char, string $name, string $img_char) : bool
    {
        $query = "
            UPDATE Characters
            SET name = :n, img_char = :i 
            WHERE id_char = :id_c
        ";

        return $this -> co -> executeQuery($query, [
            "id_c" => array($id_char, PDO::PARAM_INT),
            "name" => array($name, PDO::PARAM_STR),
            "i" => array($img_char, PDO::PARAM_STR)
        ]);
    }

}