<?php

declare(strict_types=1);

namespace App\Repository;

final class TblAdminRepository
{
    private \PDO $database;

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

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

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

        return $TblAdmin;
    }

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

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

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

        $statement->execute();

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

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

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

        $statement->execute();

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

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