<?php

declare(strict_types=1);

namespace App\Repository;

final class TblValorTipoRepository
{
    private \PDO $database;

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

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

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

        return $TblValorTipo;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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