<?php

declare(strict_types=1);

namespace App\Repository;

final class TblSegmentoCursoRepository
{
    private \PDO $database;

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

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

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

        return $TblSegmentoCurso;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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