<?php
class Database{
  
    public $conn;
    
    public function getConnection(){
  
        $this->conn = null;
  
        try {
            $this->conn = new mysqli('localhost',$this->getUsername(),$this->getPassword(),$this->getDBName());
        } catch (Exception $e) {
            echo "DB Exception: ",$e->getMessage(),"\n";
        }

        return $this->conn;
    }
    
    function parse_ini_file_custom($filename) {
        $config = [];
        $lines = file($filename, FILE_IGNORE_NEW_LINES);
        foreach ($lines as $line) {
            if (strpos($line, '=') !== false) {
                list($key, $value) = explode('=', $line, 2);
                $config[trim($key)] = trim($value);
            }
        }
        return $config;
    }
    
    function getUsername(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $userName = $envVariables['USERNAME'];
                return $userName;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
    
    function getPassword(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $passWord = $envVariables['PASSWORD'];
                return $passWord;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
    
    function getDBName(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $dbName = $envVariables['DBNAME'];
                return $dbName;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
    
    public function getEmailConnection(){
        $mail_connect = [];
        
        if(empty($this->getHost()) || empty($this->getPort()) || empty($this->getMailUsername()) || empty($this->getMailPassword())){
            return false;
        }else{
            $mail_connect["host"] = $this->getHost();
            $mail_connect["port"] = $this->getPort();
            $mail_connect["mailUsername"] = $this->getMailUsername();
            $mail_connect["mailPassword"] = $this->getMailPassword();
            
            return $mail_connect;
        }
    }
    
    function getHost(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $userName = $envVariables['MAIL_HOST'];
                return $userName;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
    
    function getPort(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $userName = $envVariables['MAIL_PORT'];
                return $userName;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
    
    function getMailUsername(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $userName = $envVariables['MAIL_USERNAME'];
                return $userName;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
    
    function getMailPassword(){
        $envFile = __DIR__ . '/../../../.env'; // Adjust the path as needed
        if (file_exists($envFile)) {
            $envVariables = $this->parse_ini_file_custom($envFile);
            if ($envVariables) {
                $userName = $envVariables['MAIL_PASSWORD'];
                return $userName;
            } else {
                echo "Error parsing .env file.";
                return;
            }
        } else {
            echo ".env file not found.";
            return;
        }
    }
}
?>