<?php

use App\Enum\NotificationTypeEnum;
use App\Models\Company;
use App\Repositories\SettingRepository;

if (!function_exists('currentCompany')) {
    /**
     * The Company resolved for this request by ResolveTenant, or null when
     * the request hit the platform's root domain (no tenant bound) — e.g.
     * the SaaS control panel, or a fresh install before any company exists.
     */
    function currentCompany(): ?Company
    {
        return app()->bound('currentCompany') ? app('currentCompany') : null;
    }
}

if (!function_exists('currentCompanyId')) {
    function currentCompanyId(): ?int
    {
        return currentCompany()?->id;
    }
}

if (!function_exists('tenantRootDomain')) {
    /**
     * The bare host companies get their handed-out subdomain under, e.g.
     * "ibi-institutes.com" so a new company "acme" resolves at
     * "acme.ibi-institutes.com". Derived from APP_URL rather than
     * hardcoded, so it follows whatever domain is actually deployed.
     */
    function tenantRootDomain(): string
    {
        return parse_url(config('app.url'), PHP_URL_HOST) ?: 'localhost';
    }
}

if (!function_exists('currency')) {
    function currency($amount)
    {
        $setting = SettingRepository::query()->get()->first();
        $currencySymbol = config('app.currency_symbol');
        $currencyPosition = $setting?->currency_position ?? "Left";
        $finalPosition = number_format($amount, 2) . $currencySymbol;

        if ($currencyPosition == 'Left') {
            $finalPosition = $currencySymbol . number_format($amount, 2);
        }

        return $finalPosition;
    }
}

if (!function_exists('filterPermission')) {
    function filterPermission($name)
    {
        if ($name == "index") {
            return "list";
        }
        if ($name == "destroy") {
            return "delete";
        }
        if ($name == "free") {
            return "course free";
        }

        if ($name == "select_course") {
            return "show course list";
        }

        if ($name == "assign_roletopermission") {
            return "assign role to permission";
        }

        if ($name == "get_permission") {
            return "get permission";
        }
        return $name;
    }
}


if (!function_exists('filterNotificationType')) {

    function filterNotificationType($type)
    {

        $getSpaceBetween = str_replace('_', ' ', $type?->value);
        $filterType = ucWords(strtolower($getSpaceBetween));
        return  $filterType;
    }
}
