<?php
namespace App\Library;
use App\Core\Service\Logger;
class Utilities 
{
    public static function getDateDiff(string $start_date, string $end_date): int{
        try {
            if(empty($start_date) || empty($end_date)) {
                throw new \Exception("Start date or end date cannot be empty.");
            }
            $start = new \DateTime($start_date);
            $end = new \DateTime($end_date);
            $diff = $start->diff($end);
            return $diff->days;
        } catch (\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line' => $e->getLine()]);
            return 0;  
        }
    }

    public static function getExportColumnFieldAlias(Array $selected_cols, Array $lis_columns) {
        try {
            $export_cols = [];
            foreach($lis_columns as $column) {
                if(!empty($column['data_column']) && !empty($column['title'])) {
                    if(in_array($column['data_column'], $selected_cols)) {
                        $export_cols[$column['data_column']] = $column['title'];
                    }
                }
            }
            return $export_cols;
        } catch(\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line' => $e->getLine()]);
            return false;  
        }
    }

    public static function getCsvExportPath(String $filename) {
        return rtrim($_ENV['MYSQL_EXPORT_PATH'], '/') . '/' . $filename; 
    }

    public static function getCsvDownloadPath(String $filename) {
        return rtrim($_ENV['EXPORT_PATH'], '/') . '/' . rtrim($_ENV['EXPORT_FOLDER'], '/') . '/' . $filename; 
    }
    public static function getPdfDownloadPath(String $filename) {
        return rtrim($_ENV['EXPORT_PATH'], '/') . '/' . rtrim($_ENV['EXPORT_FOLDER'], '/') . '/' . $filename; 
    }
    public static function getCsvRelativePath(String $filename) {
        return rtrim($_ENV['EXPORT_FOLDER'], '/') . '/' . $filename; 
    }
    
    public function random_string($type = 'alnum', $len = 8)
    {

        switch ($type) {
            case 'basic':
                return mt_rand();
            case 'alnum':
            case 'numeric':
            case 'nozero':
            case 'alpha':
                switch ($type) {
                    case 'alpha':
                        $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                        break;
                    case 'alnum':
                        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                        break;
                    case 'numeric':
                        $pool = '0123456789';
                        break;
                    case 'nozero':
                        $pool = '123456789';
                        break;
                }
                return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
            case 'unique': // todo: remove in 3.1+
            case 'md5':
                return md5(uniqid(mt_rand()));
            case 'encrypt': // todo: remove in 3.1+
            case 'sha1':
                return sha1(uniqid(mt_rand(), TRUE));
        }
    }
    
    public function showEmail($email)
    {
        if ($email != '') {
            $parts = explode("@", $email);
            $domain = array_pop($parts);
            $name = implode("@", $parts);
            $maskedName = substr($name, 0, 2) . str_repeat('*', strlen($name) - 2);
            return $maskedName . "@" . $domain;
        } else {
            return $email;
        }
    }

    public static function replaceVariables(Array $vars, Array $data) {
        try {
            $base_key = array_keys($vars);
            $key_val  = array_values($vars);
            foreach($data as $k=>$v) {
                if(is_array($v)) {
                    foreach($v as $k1=>$v1) {
                        $v[$k1] = trim(str_replace($base_key, $key_val, $v1));
                    }
                    $data[$k] = $v;
                } else {
                    $data[$k] = trim(str_replace($base_key, $key_val, $v));
                }
            }
            return ["status"=>true, "data"=>$data];
        } catch(\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line'=>$e->getLine()]);
            return ["status"=>false, "error"=>$e->getMessage()];
        }
    }

    public static function getClientToken() {
        try {
            $rstr  = bin2hex(random_bytes(32));
            $token = base64_encode($rstr);
            return ["status"=>true, "data"=>$token];
        } catch(\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line'=>$e->getLine()]);
            return ["status"=>false, "error"=>$e->getMessage()];
        }
    }

    
}
