<?php

declare(strict_types=1);

namespace App\Repository;

final class TblCiudadRepository
{
    private \PDO $database;

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

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

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

        return $TblCiudad;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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