<?php

declare(strict_types=1);

namespace App\Repository;

final class TblRolPaisRepository
{
    private \PDO $database;

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

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

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

        return $TblRolPais;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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