<?php

declare(strict_types=1);

namespace App\Repository;

final class TblTipoRepository
{
    private \PDO $database;

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

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

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

        return $TblTipo;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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