<?php

namespace src\Service;

use src\Conexao\Conexao;

class geralService
{
    private $conn;

    public function __construct()
    {
        $conexao = new Conexao();
        $this->conn = $conexao->getConexao();
    }

    public function localizar($id) {

        $query = "SELECT * FROM geral WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        if (!$stmt) {
            throw new \Exception("Erro ao preparar a consulta: " . $this->conn->error);
        }

        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows === 0) {
            throw new \Exception("Configurações gerais não encontrado");
        }

        $geral = $result->fetch_object();
        $stmt->close();

        return $geral;
    }

    // public function salvar(\src\Model\geralModel $geral)
    // {
    //     $conexao = new \src\Conexao\Conexao();
    //     $conn = $conexao->getConexao();

    //     $query = "INSERT INTO geral (urlConexaoWhatsApp, licenca, idInstancia) VALUES (?, ?, ?)";

    //     $stmt = $conn->prepare($query);
    //     if (!$stmt) {
    //         echo "Erro ao preparar a consulta: " . $conn->error;
    //         return null;
    //     }

    //     $urlConexaoWhatsApp = $geral->getUrlConexaoWhatsApp();
    //     $licenca = $geral->getLicenca();
    //     $idInstancia = $geral->getIdInstancia();

    //     $stmt->bind_param('sss', $urlConexaoWhatsApp, $licenca, $idInstancia);

    //     $stmt->execute();
    // }

    // public function atualizar(\src\Model\geralModel $geral)
    // {
    //     $conexao = new \src\Conexao\Conexao();
    //     $conn = $conexao->getConexao();

    //     $query = "UPDATE geral SET urlConexaoWhatsApp = ?, licenca = ?, idInstancia = ? WHERE id = ?";

    //     $stmt = $conn->prepare($query);
    //     if (!$stmt) {
    //         echo "Erro ao preparar a consulta: " . $conn->error;
    //         return null;
    //     }

    //     $urlConexaoWhatsApp = $geral->getUrlConexaoWhatsApp();
    //     $licenca = $geral->getLicenca();
    //     $idInstancia = $geral->getIdInstancia();
    //     $id = $geral->getId();

    //     $stmt->bind_param(
    //         'sssi',
    //         $urlConexaoWhatsApp,
    //         $licenca,
    //         $idInstancia,
    //         $id
    //     );

    //     $stmt->execute();
    // }

    // public function localizar($id)
    // {
    //     $conexao = new \src\Conexao\Conexao();
    //     $conn = $conexao->getConexao();

    //     $query = "SELECT * FROM geral WHERE id = ?";

    //     $stmt = $conn->prepare($query);
    //     if (!$stmt) {
    //         echo "Erro ao preparar a consulta: " . $conn->error;
    //         return null;
    //     }

    //     $stmt->bind_param('i', $id);
    //     $stmt->execute();

    //     $result = $stmt->get_result();
    //     $geral = $result->fetch_object();

    //     return $geral;
    // }
}
