<?php

declare(strict_types=1);

namespace App\Repository;

final class TblSegmentoUsuarioRepository
{
    private \PDO $database;

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

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

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

        return $TblSegmentoUsuario;
    }

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

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

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

        $statement->execute();

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

    public function update(object $TblSegmentoUsuario, object $data): object
    {
        if (isset($data->fecha)) {
            $TblSegmentoUsuario->fecha = $data->fecha;
        }
        if (isset($data->IdUsuario)) {
            $TblSegmentoUsuario->IdUsuario = $data->IdUsuario;
        }
        if (isset($data->IdSegmento)) {
            $TblSegmentoUsuario->IdSegmento = $data->IdSegmento;
        }
        if (isset($data->Fecha2)) {
            $TblSegmentoUsuario->Fecha2 = $data->Fecha2;
        }
        if (isset($data->field1)) {
            $TblSegmentoUsuario->field1 = $data->field1;
        }
        if (isset($data->field2)) {
            $TblSegmentoUsuario->field2 = $data->field2;
        }
        if (isset($data->field3)) {
            $TblSegmentoUsuario->field3 = $data->field3;
        }
        if (isset($data->field4)) {
            $TblSegmentoUsuario->field4 = $data->field4;
        }
        if (isset($data->field5)) {
            $TblSegmentoUsuario->field5 = $data->field5;
        }

        $query = 'UPDATE `TblSegmentoUsuario` SET `fecha` = :fecha, `IdUsuario` = :IdUsuario, `IdSegmento` = :IdSegmento, `Fecha2` = :Fecha2, `field1` = :field1, `field2` = :field2, `field3` = :field3, `field4` = :field4, `field5` = :field5 WHERE `id` = :id';
        $statement = $this->getDb()->prepare($query);
        $statement->bindParam('id', $TblSegmentoUsuario->id);
        $statement->bindParam('fecha', $TblSegmentoUsuario->fecha);
        $statement->bindParam('IdUsuario', $TblSegmentoUsuario->IdUsuario);
        $statement->bindParam('IdSegmento', $TblSegmentoUsuario->IdSegmento);
        $statement->bindParam('Fecha2', $TblSegmentoUsuario->Fecha2);
        $statement->bindParam('field1', $TblSegmentoUsuario->field1);
        $statement->bindParam('field2', $TblSegmentoUsuario->field2);
        $statement->bindParam('field3', $TblSegmentoUsuario->field3);
        $statement->bindParam('field4', $TblSegmentoUsuario->field4);
        $statement->bindParam('field5', $TblSegmentoUsuario->field5);

        $statement->execute();

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

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