<?php

declare(strict_types=1);

namespace App\Repository;

final class TblNotificacionRepository
{
    private \PDO $database;

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

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

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

        return $TblNotificacion;
    }

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

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

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

        $statement->execute();

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

    public function update(object $TblNotificacion, object $data): object
    {
        if (isset($data->fecha)) {
            $TblNotificacion->fecha = $data->fecha;
        }
        if (isset($data->Titulo)) {
            $TblNotificacion->Titulo = $data->Titulo;
        }
        if (isset($data->Notificacion)) {
            $TblNotificacion->Notificacion = $data->Notificacion;
        }
        if (isset($data->TipoNotificacion)) {
            $TblNotificacion->TipoNotificacion = $data->TipoNotificacion;
        }
        if (isset($data->Accion)) {
            $TblNotificacion->Accion = $data->Accion;
        }
        if (isset($data->field1)) {
            $TblNotificacion->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblNotificacion->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblNotificacion->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblNotificacion->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblNotificacion->field5 = $data->field5;
        }

        $query = 'UPDATE `TblNotificacion` SET `fecha` = :fecha, `Titulo` = :Titulo, `Notificacion` = :Notificacion, `TipoNotificacion` = :TipoNotificacion, `Accion` = :Accion, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblNotificacion->id);
        $statement->bindParam('fecha', $TblNotificacion->fecha);
        $statement->bindParam('Titulo', $TblNotificacion->Titulo);
        $statement->bindParam('Notificacion', $TblNotificacion->Notificacion);
        $statement->bindParam('TipoNotificacion', $TblNotificacion->TipoNotificacion);
        $statement->bindParam('Accion', $TblNotificacion->Accion);
        $statement->bindParam('field1', $TblNotificacion->field1);
        $statement->bindParam('field2', $TblNotificacion->field2);
        $statement->bindParam('field3', $TblNotificacion->field3);
        $statement->bindParam('field4', $TblNotificacion->field4);
        $statement->bindParam('field5', $TblNotificacion->field5);

        $statement->execute();

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

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