<?php

declare(strict_types=1);

namespace App\Repository;

final class TblDepartamentoRepository
{
    private \PDO $database;

    public function __construct(\PDO $database)
    {
        $this->database = $database;
    }

    public function getDb(): \PDO
    {
        return $this->database;
    }

    public function checkAndGet(int $TblDepartamentoId): object
    {
        $query = 'SELECT * FROM `TblDepartamento` WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblDepartamentoId);
        $statement->execute();
        $TblDepartamento = $statement->fetchObject();
        if (! $TblDepartamento) {
            throw new \Exception('TblDepartamento not found.', 404);
        }

        return $TblDepartamento;
    }

    public function getAll(): array
    {
        $query = 'SELECT * FROM `TblDepartamento` ORDER BY `id`';
        $statement = $this->getDb()->prepare($query);
        $statement->execute();

        return (array) $statement->fetchAll();
    }

    public function create(object $TblDepartamento): object
    {
        $query = 'INSERT INTO `TblDepartamento` (`Nombre`, `IdPais`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (:Nombre, :IdPais, :field1, :field2, :field3, :field4, :field5)';
        $statement = $this->getDb()->prepare($query);
//         $statement->bindParam('id', $TblDepartamento->id);
//         $statement->bindParam('fecha', $TblDepartamento->fecha);
        $statement->bindParam('Nombre', $TblDepartamento->Nombre);
        $statement->bindParam('IdPais', $TblDepartamento->IdPais);
        $statement->bindParam('field1', $TblDepartamento->field1);
        $statement->bindParam('field2', $TblDepartamento->field2);
        $statement->bindParam('field3', $TblDepartamento->field3);
        $statement->bindParam('field4', $TblDepartamento->field4);
        $statement->bindParam('field5', $TblDepartamento->field5);

        $statement->execute();

        return $this->checkAndGet((int) $this->getDb()->lastInsertId());
    }

    public function update(object $TblDepartamento, object $data): object
    {
        if (isset($data->fecha)) {
            $TblDepartamento->fecha = $data->fecha;
        }
        if (isset($data->Nombre)) {
            $TblDepartamento->Nombre = $data->Nombre;
        }
        if (isset($data->IdPais)) {
            $TblDepartamento->IdPais = $data->IdPais;
        }
        if (isset($data->field1)) {
            $TblDepartamento->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblDepartamento->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblDepartamento->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblDepartamento->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblDepartamento->field5 = $data->field5;
        }

        $query = 'UPDATE `TblDepartamento` SET `fecha` = :fecha, `Nombre` = :Nombre, `IdPais` = :IdPais, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblDepartamento->id);
        $statement->bindParam('fecha', $TblDepartamento->fecha);
        $statement->bindParam('Nombre', $TblDepartamento->Nombre);
        $statement->bindParam('IdPais', $TblDepartamento->IdPais);
        $statement->bindParam('field1', $TblDepartamento->field1);
        $statement->bindParam('field2', $TblDepartamento->field2);
        $statement->bindParam('field3', $TblDepartamento->field3);
        $statement->bindParam('field4', $TblDepartamento->field4);
        $statement->bindParam('field5', $TblDepartamento->field5);

        $statement->execute();

        return $this->checkAndGet((int) $TblDepartamento->id);
    }

    public function delete(int $TblDepartamentoId): void
    {
        $query = 'DELETE FROM `TblDepartamento` WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblDepartamentoId);
        $statement->execute();
    }
}
