<?php

declare(strict_types=1);

namespace App\Repository;

final class TblNoticiaGenfarRepository
{
    private \PDO $database;

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

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

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

        return $TblNoticiaGenfar;
    }

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

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

    public function create(object $TblNoticiaGenfar): object
    {
        $query = 'INSERT INTO `TblNoticiaGenfar` (`Titulo`, `Descripcion`, `IdTipoNoticiaGenfar`, `FechaInicio`, `FechaFin`, `UrlPagina`, `UrlImagen`, `FechaCreacion`, `field1`, `field2`, 
        `field3`, `field4`, `field5`) VALUES (:Titulo, :Descripcion, :IdTipoNoticiaGenfar, :FechaInicio, :FechaFin, :UrlPagina, :UrlImagen, :FechaCreacion, :field1, :field2, :field3, :field4, :field5)';
        $statement = $this->getDb()->prepare($query);
//         $statement->bindParam('id', $TblNoticiaGenfar->id);
//         $statement->bindParam('fecha', $TblNoticiaGenfar->fecha);
        $statement->bindParam('Titulo', $TblNoticiaGenfar->Titulo);
        $statement->bindParam('Descripcion', $TblNoticiaGenfar->Descripcion);
        $statement->bindParam('IdTipoNoticiaGenfar', $TblNoticiaGenfar->IdTipoNoticiaGenfar);
        $statement->bindParam('FechaInicio', $TblNoticiaGenfar->FechaInicio);
        $statement->bindParam('FechaFin', $TblNoticiaGenfar->FechaFin);
        $statement->bindParam('UrlPagina', $TblNoticiaGenfar->UrlPagina);
        $statement->bindParam('UrlImagen', $TblNoticiaGenfar->UrlImagen);
        $statement->bindParam('FechaCreacion', $TblNoticiaGenfar->FechaCreacion);
        $statement->bindParam('field1', $TblNoticiaGenfar->field1);
        $statement->bindParam('field2', $TblNoticiaGenfar->field2);
        $statement->bindParam('field3', $TblNoticiaGenfar->field3);
        $statement->bindParam('field4', $TblNoticiaGenfar->field4);
        $statement->bindParam('field5', $TblNoticiaGenfar->field5);

        $statement->execute();

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

    public function update(object $TblNoticiaGenfar, object $data): object
    {
        if (isset($data->fecha)) {
            $TblNoticiaGenfar->fecha = $data->fecha;
        }
        if (isset($data->Titulo)) {
            $TblNoticiaGenfar->Titulo = $data->Titulo;
        }
        if (isset($data->Descripcion)) {
            $TblNoticiaGenfar->Descripcion = $data->Descripcion;
        }
        if (isset($data->IdTipoNoticiaGenfar)) {
            $TblNoticiaGenfar->IdTipoNoticiaGenfar = $data->IdTipoNoticiaGenfar;
        }
        if (isset($data->FechaInicio)) {
            $TblNoticiaGenfar->FechaInicio = $data->FechaInicio;
        }
        if (isset($data->FechaFin)) {
            $TblNoticiaGenfar->FechaFin = $data->FechaFin;
        }
        if (isset($data->UrlPagina)) {
            $TblNoticiaGenfar->UrlPagina = $data->UrlPagina;
        }
        if (isset($data->UrlImagen)) {
            $TblNoticiaGenfar->UrlImagen = $data->UrlImagen;
        }
        if (isset($data->FechaCreacion)) {
            $TblNoticiaGenfar->FechaCreacion = $data->FechaCreacion;
        }
        if (isset($data->field1)) {
            $TblNoticiaGenfar->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblNoticiaGenfar->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblNoticiaGenfar->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblNoticiaGenfar->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblNoticiaGenfar->field5 = $data->field5;
        }

        $query = 'UPDATE `TblNoticiaGenfar` SET `fecha` = :fecha, `Titulo` = :Titulo, `Descripcion` = :Descripcion, `IdTipoNoticiaGenfar` = :IdTipoNoticiaGenfar, `FechaInicio` = :FechaInicio, `FechaFin` = :FechaFin, `UrlPagina` = :UrlPagina, `UrlImagen` = :UrlImagen, `FechaCreacion` = :FechaCreacion, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblNoticiaGenfar->id);
        $statement->bindParam('fecha', $TblNoticiaGenfar->fecha);
        $statement->bindParam('Titulo', $TblNoticiaGenfar->Titulo);
        $statement->bindParam('Descripcion', $TblNoticiaGenfar->Descripcion);
        $statement->bindParam('IdTipoNoticiaGenfar', $TblNoticiaGenfar->IdTipoNoticiaGenfar);
        $statement->bindParam('FechaInicio', $TblNoticiaGenfar->FechaInicio);
        $statement->bindParam('FechaFin', $TblNoticiaGenfar->FechaFin);
        $statement->bindParam('UrlPagina', $TblNoticiaGenfar->UrlPagina);
        $statement->bindParam('UrlImagen', $TblNoticiaGenfar->UrlImagen);
        $statement->bindParam('FechaCreacion', $TblNoticiaGenfar->FechaCreacion);
        $statement->bindParam('field1', $TblNoticiaGenfar->field1);
        $statement->bindParam('field2', $TblNoticiaGenfar->field2);
        $statement->bindParam('field3', $TblNoticiaGenfar->field3);
        $statement->bindParam('field4', $TblNoticiaGenfar->field4);
        $statement->bindParam('field5', $TblNoticiaGenfar->field5);

        $statement->execute();

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

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