<?php

declare(strict_types=1);

namespace App\Repository;

final class TblTipoUsuarioRepository
{
    private \PDO $database;

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

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

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

        return $TblTipoUsuario;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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