<?php

declare(strict_types=1);

namespace App\Repository;

final class TblSupervisorCiudadRepository
{
    private \PDO $database;

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

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

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

        return $TblSupervisorCiudad;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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