<?php

declare(strict_types=1);

namespace App\Repository;

final class TblRolUsuarioRepository
{
    private \PDO $database;

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

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

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

        return $TblRolUsuario;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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