<?php

use Carbon\Carbon;

if (!function_exists('current_route')) {
    function current_route()
    {
        $router = \CodeIgniter\Config\Services::router();
        return $router->getMatchedRouteOptions()['as'];
    }
}

if (!function_exists('get_user')) {
    function get_user(): object|array|null
    {
        $model = model('App\Models\UserModel');
        return $model->asObject()
            ->where('id', \App\Libraries\UserAuth::id())
            ->first();
    }
}
if (!function_exists('get_address')) {
    function get_address(): object|array|null
    {
        $model = model('App\Models\SetupModel');
        return $model->getData(1);
    }
}
if (!function_exists('uniqueKey')) {
    function uniqueKey($prefix = null): string
    {
        $uniqueKey = uniqid($prefix ?? "", true);
        return str_replace(['-', '.'], '', $uniqueKey);
    }
}
if (!function_exists('sendEmail')) {
    function sendEmail($data)
    {
        // $link = url_to('set_password', $data['id']);
        // $user = join(' ', [$data['data']['first_name'], $data['data']['last_name']]);
        // $email = $data['data']['email'];
        // $msg = sprintf(EMAIL_CONFIRMATION, $user, $link);

        // $email = Config\Services::email();
        // $email->setTo($data['data']['email']);
        // $email->setSubject('Email Verification - ' . APP_NAME);
        // $email->setMessage($msg);

        // return $email->send();
    }
}
if (!function_exists('sendResetEmail')) {
    function sendResetEmail($data)
    {
        // $link = url_to('set_password', $data['id']);
        // $user = join(' ', [$data['data']['first_name'], $data['data']['last_name']]);
        // $email = $data['data']['email'];
        // $msg = sprintf(EMAIL_CONFIRMATION, $user, $link);

        // $email = Config\Services::email();
        // $email->setTo($data['data']['email']);
        // $email->setSubject('Email Verification - ' . APP_NAME);
        // $email->setMessage($msg);

        // return $email->send();
    }
}
if (!function_exists('is_token_expired')) {
    function is_token_expired($datetime): bool
    {
        $difference = Carbon::createFromFormat('Y-m-d H:i:s', $datetime)->diffInMinutes(Carbon::now());
        return $difference > env('LINK_EXPIRY');
    }
}
