<?php

declare(strict_types=1);

namespace App\Repository;

final class TblNotificacionPaisRepository
{
    private \PDO $database;

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

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

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

        return $TblNotificacionPais;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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