<?php

declare(strict_types=1);

namespace App\Repository;

final class TblTemaRepository
{
    private \PDO $database;

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

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

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

        return $TblTema;
    }

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

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

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

        $statement->execute();

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

    public function update(object $TblTema, object $data): object
    {
        if (isset($data->fecha)) {
            $TblTema->fecha = $data->fecha;
        }
        if (isset($data->Tema)) {
            $TblTema->Tema = $data->Tema;
        }
        if (isset($data->Imagen)) {
            $TblTema->Imagen = $data->Imagen;
        }
        if (isset($data->IdModulo)) {
            $TblTema->IdModulo = $data->IdModulo;
        }
        if (isset($data->Codigo)) {
            $TblTema->Codigo = $data->Codigo;
        }
        if (isset($data->TiempoDuracion)) {
            $TblTema->TiempoDuracion = $data->TiempoDuracion;
        }
        if (isset($data->NumeroIntentosPermitidos)) {
            $TblTema->NumeroIntentosPermitidos = $data->NumeroIntentosPermitidos;
        }
        if (isset($data->field1)) {
            $TblTema->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblTema->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblTema->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblTema->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblTema->field5 = $data->field5;
        }

        $query = 'UPDATE `TblTema` SET `fecha` = :fecha, `Tema` = :Tema, `Imagen` = :Imagen, `IdModulo` = :IdModulo, `Codigo` = :Codigo, `TiempoDuracion` = :TiempoDuracion, `NumeroIntentosPermitidos` = :NumeroIntentosPermitidos, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblTema->id);
        $statement->bindParam('fecha', $TblTema->fecha);
        $statement->bindParam('Tema', $TblTema->Tema);
        $statement->bindParam('Imagen', $TblTema->Imagen);
        $statement->bindParam('IdModulo', $TblTema->IdModulo);
        $statement->bindParam('Codigo', $TblTema->Codigo);
        $statement->bindParam('TiempoDuracion', $TblTema->TiempoDuracion);
        $statement->bindParam('NumeroIntentosPermitidos', $TblTema->NumeroIntentosPermitidos);
        $statement->bindParam('field1', $TblTema->field1);
        $statement->bindParam('field2', $TblTema->field2);
        $statement->bindParam('field3', $TblTema->field3);
        $statement->bindParam('field4', $TblTema->field4);
        $statement->bindParam('field5', $TblTema->field5);

        $statement->execute();

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

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