<?php

declare(strict_types=1);

namespace App\Repository;

final class TblSegmentoRepository
{
    private \PDO $database;

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

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

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

        return $TblSegmento;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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