<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Serveur extends Model
{
    use HasFactory;

    public static function setDatabaseConnection($accountAlias) {
        // Extract code_membre from accountAlias
        $code_membre = substr($accountAlias, -11);

        // Check if code_membre exists in the INSCRIPTIONINTERCAISSE table
        $nb_row = DB::connection('sqlsrv')
                    ->table('INSCRIPTIONINTERCAISSE')
                    ->where('CODEMEMBRE', $code_membre)
                    ->count();

        if ($nb_row > 0) {
            // Retrieve the member's information
            $info = DB::connection('sqlsrv')
                      ->table('INSCRIPTIONINTERCAISSE')
                      ->where('CODEMEMBRE', $code_membre)
                      ->first();

            Log::info("CODE AGENCE PINK : ".$info->CODEAGENCE);

            // Retrieve server information based on the agency code
            $info_serveur = DB::table('serveurs')
                              ->where('code_agence', $info->CODEAGENCE)
                              ->orderBy('id', 'desc')
                              ->first();

            if ($info_serveur) {
                // Set a new database connection with dynamic configuration
                try {
                    Config::set("database.connections.sqlsrv2", [
                        "driver" => "sqlsrv",
                        "host" => $info_serveur->ip_agence,
                        "port" => $info_serveur->port,
                        "database" => $info_serveur->bd_agence,
                        "username" => $info_serveur->login_sa,
                        "password" => Crypt::decryptString($info_serveur->password),
                    ]);
                    // Log successful connection
                    Log::info("Database connection established successfully");
                    return true;
                } catch (\Exception $e) {
                    // Log or handle the error
                    \App\Helpers\LogHandler::logError($e); // Log the error
                    Log::error("Database connection failed: " . $e->getMessage());
                    return false;
                }


                    // Return true to indicate success


            }
        }

        // Return false if code_membre was not found or server info is missing
        return false;
    }

public static function setDatabaseConnectionByAg($agence_code) {


            // Retrieve server information based on the agency code
            $info_serveur = DB::table('serveurs')
                              ->where('code_agence', $agence_code)
                              ->orderBy('id', 'desc')
                              ->first();

            if ($info_serveur) {
                // Set a new database connection with dynamic configuration
                try {
                    Config::set("database.connections.sqlsrv2", [
                        "driver" => "sqlsrv",
                        "host" => $info_serveur->ip_agence,
                        "port" => $info_serveur->port,
                        "database" => $info_serveur->bd_agence,
                        "username" => $info_serveur->login_sa,
                        "password" => Crypt::decryptString($info_serveur->password),
                    ]);
                    // Log successful connection
                    Log::info("Database connection established successfully");
                    return true;
                } catch (\Exception $e) {
                    // Log or handle the error
                    \App\Helpers\LogHandler::logError($e); // Log the error
                    Log::error("Database connection failed: " . $e->getMessage());
                    return false;
                }


                    // Return true to indicate success


            }


        // Return false if code_membre was not found or server info is missing
        return false;
    }

}
