<?php

declare(strict_types=1);

namespace App\Repository;

final class TblModuloRepository
{
    private \PDO $database;

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

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

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

        return $TblModulo;
    }

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

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

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

        $statement->execute();

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

    public function update(object $TblModulo, object $data): object
    {
        if (isset($data->fecha)) {
            $TblModulo->fecha = $data->fecha;
        }
        if (isset($data->Modulo)) {
            $TblModulo->Modulo = $data->Modulo;
        }
        if (isset($data->Imagen)) {
            $TblModulo->Imagen = $data->Imagen;
        }
        if (isset($data->Orden)) {
            $TblModulo->Orden = $data->Orden;
        }
        if (isset($data->IdCurso)) {
            $TblModulo->IdCurso = $data->IdCurso;
        }
        if (isset($data->field1)) {
            $TblModulo->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblModulo->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblModulo->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblModulo->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblModulo->field5 = $data->field5;
        }

        $query = 'UPDATE `TblModulo` SET `fecha` = :fecha, `Modulo` = :Modulo, `Imagen` = :Imagen, `Orden` = :Orden, `IdCurso` = :IdCurso, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblModulo->id);
        $statement->bindParam('fecha', $TblModulo->fecha);
        $statement->bindParam('Modulo', $TblModulo->Modulo);
        $statement->bindParam('Imagen', $TblModulo->Imagen);
        $statement->bindParam('Orden', $TblModulo->Orden);
        $statement->bindParam('IdCurso', $TblModulo->IdCurso);
        $statement->bindParam('field1', $TblModulo->field1);
        $statement->bindParam('field2', $TblModulo->field2);
        $statement->bindParam('field3', $TblModulo->field3);
        $statement->bindParam('field4', $TblModulo->field4);
        $statement->bindParam('field5', $TblModulo->field5);

        $statement->execute();

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

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