<?php

declare(strict_types=1);

namespace App\Repository;

final class TblNoticiaGenfarPaisRepository
{
    private \PDO $database;

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

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

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

        return $TblNoticiaGenfarPais;
    }

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

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

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

        $statement->execute();

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

    public function update(object $TblNoticiaGenfarPais, object $data): object
    {
        if (isset($data->fecha)) {
            $TblNoticiaGenfarPais->fecha = $data->fecha;
        }
        if (isset($data->IdNoticiaGenfar)) {
            $TblNoticiaGenfarPais->IdNoticiaGenfar = $data->IdNoticiaGenfar;
        }
        if (isset($data->IdPais)) {
            $TblNoticiaGenfarPais->IdPais = $data->IdPais;
        }
        if (isset($data->field1)) {
            $TblNoticiaGenfarPais->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblNoticiaGenfarPais->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblNoticiaGenfarPais->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblNoticiaGenfarPais->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblNoticiaGenfarPais->field5 = $data->field5;
        }

        $query = 'UPDATE `TblNoticiaGenfarPais` SET `fecha` = :fecha, `IdNoticiaGenfar` = :IdNoticiaGenfar, `IdPais` = :IdPais, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblNoticiaGenfarPais->id);
        $statement->bindParam('fecha', $TblNoticiaGenfarPais->fecha);
        $statement->bindParam('IdNoticiaGenfar', $TblNoticiaGenfarPais->IdNoticiaGenfar);
        $statement->bindParam('IdPais', $TblNoticiaGenfarPais->IdPais);
        $statement->bindParam('field1', $TblNoticiaGenfarPais->field1);
        $statement->bindParam('field2', $TblNoticiaGenfarPais->field2);
        $statement->bindParam('field3', $TblNoticiaGenfarPais->field3);
        $statement->bindParam('field4', $TblNoticiaGenfarPais->field4);
        $statement->bindParam('field5', $TblNoticiaGenfarPais->field5);

        $statement->execute();

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

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