<?php

declare(strict_types=1);

namespace App\Repository;

final class TblTipoNoticiaGenfarRepository
{
    private \PDO $database;

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

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

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

        return $TblTipoNoticiaGenfar;
    }

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

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

    public function create(object $TblTipoNoticiaGenfar): object
    {
        $query = 'INSERT INTO `TblTipoNoticiaGenfar` (`TipoNoticia`, `Estado`, `Icono`, `Color`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (:TipoNoticia, :Estado, :Icono, :Color, :field1, :field2, :field3, :field4, :field5)';
        $statement = $this->getDb()->prepare($query);
//         $statement->bindParam('id', $TblTipoNoticiaGenfar->id);
//         $statement->bindParam('fecha', $TblTipoNoticiaGenfar->fecha);
        $statement->bindParam('TipoNoticia', $TblTipoNoticiaGenfar->TipoNoticia);
        $statement->bindParam('Estado', $TblTipoNoticiaGenfar->Estado);
        $statement->bindParam('Icono', $TblTipoNoticiaGenfar->Icono);
        $statement->bindParam('Color', $TblTipoNoticiaGenfar->Color);
        $statement->bindParam('field1', $TblTipoNoticiaGenfar->field1);
        $statement->bindParam('field2', $TblTipoNoticiaGenfar->field2);
        $statement->bindParam('field3', $TblTipoNoticiaGenfar->field3);
        $statement->bindParam('field4', $TblTipoNoticiaGenfar->field4);
        $statement->bindParam('field5', $TblTipoNoticiaGenfar->field5);

        $statement->execute();

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

    public function update(object $TblTipoNoticiaGenfar, object $data): object
    {
        if (isset($data->fecha)) {
            $TblTipoNoticiaGenfar->fecha = $data->fecha;
        }
        if (isset($data->TipoNoticia)) {
            $TblTipoNoticiaGenfar->TipoNoticia = $data->TipoNoticia;
        }
        if (isset($data->Estado)) {
            $TblTipoNoticiaGenfar->Estado = $data->Estado;
        }
        if (isset($data->Icono)) {
            $TblTipoNoticiaGenfar->Icono = $data->Icono;
        }
        if (isset($data->Color)) {
            $TblTipoNoticiaGenfar->Color = $data->Color;
        }
        if (isset($data->field1)) {
            $TblTipoNoticiaGenfar->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblTipoNoticiaGenfar->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblTipoNoticiaGenfar->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblTipoNoticiaGenfar->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblTipoNoticiaGenfar->field5 = $data->field5;
        }

        $query = 'UPDATE `TblTipoNoticiaGenfar` SET `fecha` = :fecha, `TipoNoticia` = :TipoNoticia, `Estado` = :Estado, `Icono` = :Icono, `Color` = :Color, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblTipoNoticiaGenfar->id);
        $statement->bindParam('fecha', $TblTipoNoticiaGenfar->fecha);
        $statement->bindParam('TipoNoticia', $TblTipoNoticiaGenfar->TipoNoticia);
        $statement->bindParam('Estado', $TblTipoNoticiaGenfar->Estado);
        $statement->bindParam('Icono', $TblTipoNoticiaGenfar->Icono);
        $statement->bindParam('Color', $TblTipoNoticiaGenfar->Color);
        $statement->bindParam('field1', $TblTipoNoticiaGenfar->field1);
        $statement->bindParam('field2', $TblTipoNoticiaGenfar->field2);
        $statement->bindParam('field3', $TblTipoNoticiaGenfar->field3);
        $statement->bindParam('field4', $TblTipoNoticiaGenfar->field4);
        $statement->bindParam('field5', $TblTipoNoticiaGenfar->field5);

        $statement->execute();

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

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