<?php

declare(strict_types=1);

namespace App\Repository;

final class TblPreguntaRepository
{
    private \PDO $database;

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

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

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

        return $TblPregunta;
    }

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

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

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

        $statement->execute();

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

    public function update(object $TblPregunta, object $data): object
    {
        if (isset($data->fecha)) {
            $TblPregunta->fecha = $data->fecha;
        }
        if (isset($data->Pregunta)) {
            $TblPregunta->Pregunta = $data->Pregunta;
        }
        if (isset($data->IdTema)) {
            $TblPregunta->IdTema = $data->IdTema;
        }
        if (isset($data->Celula)) {
            $TblPregunta->Celula = $data->Celula;
        }
        if (isset($data->Codigo)) {
            $TblPregunta->Codigo = $data->Codigo;
        }
        if (isset($data->field1)) {
            $TblPregunta->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblPregunta->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblPregunta->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblPregunta->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblPregunta->field5 = $data->field5;
        }

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

        $statement->execute();

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

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