<?php

use App\Models\AppSession;

if(!function_exists('get_current_session')) {
    function get_current_session(){
        return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
    }
}


if (!function_exists('value_from_rs')) {
    function value_from_rs($rs, $field, $checks, $condition = "all") {
        $result = '-';
        switch ($condition) {
            case "all":
                foreach ($rs as $record) {
                    $failed = false;
                    foreach ($checks as $check) {
                        if($check[1] === '=') {
                            if(!($record->{$check[0]} == $check[2])) {
                                $failed = true;
                                break;
                            }
                        }
                        else if($check[1] === '!=') {
                            if(!($record->{$check[0]} != $check[2])) {
                                $failed = true;
                                break;
                            }
                        }
                    }
                    if(!$failed) {
                        $result = $record->$field;
                        break;
                    }
                }
                break;
            case "any":
                foreach ($rs as $record) {
                    foreach ($checks as $check) {
                        if($check[1] === '=') {
                            if($record->{$check[0]} == $check[2]) {
                                $result = $record->$field;
                                break;
                            }
                        }
                        else if($check[1] === '!=') {
                            if($record->{$check[0]} != $check[2]) {
                                $result = $record->$field;
                                break;
                            }
                        }
                    }
                }
                break;
            case "none":
                foreach ($rs as $record) {
                    $failed = false;
                    foreach ($checks as $check) {
                        if($check[1] === '=') {
                            if($record->{$check[0]} == $check[2]) {
                                $failed = true;
                                break;
                            }
                        }
                        else if($check[1] === '!=') {
                            if($record->{$check[0]} != $check[2]) {
                                $failed = true;
                                break;
                            }
                        }
                    }
                    if(!$failed) {
                        $result = $record->$field;
                        break;
                    }
                }
                break;
        }
        return $result;
    }
}

if(!function_exists('friendly_date_time')) {
    function friendly_date_time($value) {
        if(!$value || empty($value)) return "-";
        try {
            $result = strtotime($value);
            $result = date("j M o, H:i", $result);
            return $result;
        }
        catch (Exception $e) {
            return $value;
        }
    }
}



function zFormalName($person){
    return zImplode(', ', [$person->name_last, $person->name_first]);
}

function zImplode($glue, $pieces){
    return implode($glue, array_filter($pieces));
}