<?php

namespace Utils;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../../Logger.php';

use PDO;
use PDOException;
use Utils\Logger;

class Database
{
    private static $pdo;

    public static function conectar()
    {
        if (!isset(self::$pdo)) {
            $config = require __DIR__ . '/../Config/config.php';

            $host = $config['database']['host'];
            $dbname = $config['database']['dbname'];
            $username = $config['database']['username'];
            $password = $config['database']['password'];

            try {
                self::$pdo = new PDO("pgsql:host=$host;dbname=$dbname;user=$username;password=$password");
                self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                die("Error de conexión: " . $e->getMessage());            }
        }

        return self::$pdo;
    }
}

class Database_crm
{
    private static $pdo_crm;

    public static function conectar_crm()
    {
        if (!isset(self::$pdo_crm)) {
            $config_crm = require __DIR__ . '/../Config/config.php';

            $host_crm = $config_crm['database_crm']['host_crm'];
            $dbname_crm = $config_crm['database_crm']['dbname_crm'];
            $username_crm = $config_crm['database_crm']['username_crm'];
            $password_crm = $config_crm['database_crm']['password_crm'];

            try {
                self::$pdo_crm = new PDO("pgsql:host=$host_crm;dbname=$dbname_crm;user=$username_crm;password=$password_crm");
                self::$pdo_crm->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                die("Error de conexión: " . $e->getMessage());
            }
        }

        return self::$pdo_crm;
    }
}

class Database_tesoreria
{
    private static $pdo_tesoreria;

    public static function conectar_tesoreria()
    {
        if (!isset(self::$pdo_tesoreria)) {
            $config_tesoreria = require __DIR__ . '/../Config/config.php';

            $host_tesoreria = $config_tesoreria['database_tesoreria']['host_tesoreria'];
            $dbname_tesoreria = $config_tesoreria['database_tesoreria']['dbname_tesoreria'];
            $username_tesoreria = $config_tesoreria['database_tesoreria']['username_tesoreria'];
            $password_tesoreria = $config_tesoreria['database_tesoreria']['password_tesoreria'];

            try {
                self::$pdo_tesoreria = new PDO("pgsql:host=$host_tesoreria;dbname=$dbname_tesoreria;user=$username_tesoreria;password=$password_tesoreria");
                self::$pdo_tesoreria->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                die("Error de conexión: " . $e->getMessage());
            }
        }

        return self::$pdo_tesoreria;
    }
}
