<?php

use App\Services\SystemConfig\SystemConfigService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

// ── TAB PERMISSION HELPERS ────────────────────────────────────────────────────
// Usage: can_view_tab('global settings', 'login-security')
//        can_manage_tab('mobile settings', 'branding')
//
// Backward-compat rule: if the user holds the legacy menu-level
// 'manage {menu}' permission, they automatically pass every tab check.

if (! function_exists('can_view_tab')) {
    function can_view_tab(string $menu, string $tab): bool
    {
        if (! auth()->check()) {
            return false;
        }
        $user = auth()->user();
        $tab = str_replace('_', '-', $tab);

        // Developer gate (super-admin) — already handled by Gate::before, but
        // we check explicitly here because this is a helper, not a gate call.
        if ($user->hasRole('Developer')) {
            return true;
        }

        // Legacy menu-level manage permission grants full tab access
        if ($user->can("manage {$menu}")) {
            return true;
        }

        // Scoped view or manage grants read access to that tab
        return $user->can("view {$menu}:{$tab}")
            || $user->can("manage {$menu}:{$tab}");
    }
}

if (! function_exists('can_manage_tab')) {
    function can_manage_tab(string $menu, string $tab): bool
    {
        if (! auth()->check()) {
            return false;
        }
        $user = auth()->user();
        $tab = str_replace('_', '-', $tab);

        if ($user->hasRole('Developer')) {
            return true;
        }

        // Legacy menu-level manage = write access to all tabs
        if ($user->can("manage {$menu}")) {
            return true;
        }

        return $user->can("manage {$menu}:{$tab}");
    }
}

if (! function_exists('can_view_any_tab')) {
    /**
     * Returns true if the user can access at least one tab of a given menu.
     * Used to decide whether to show the menu item in the sidebar at all.
     */
    function can_view_any_tab(string $menu): bool
    {
        if (! auth()->check()) {
            return false;
        }
        $user = auth()->user();

        if ($user->hasRole('Developer')) {
            return true;
        }
        if ($user->can("view {$menu}") || $user->can("manage {$menu}")) {
            return true;
        }

        // Check granular permissions (both direct and role-based)
        return $user->getAllPermissions()
            ->contains(fn ($p) => str_starts_with($p->name, "view {$menu}:") || str_starts_with($p->name, "manage {$menu}:"));
    }
}

if (! function_exists('can_manage_any_tab')) {
    /**
     * Returns true if the user can manage at least one tab of a given menu.
     */
    function can_manage_any_tab(string $menu): bool
    {
        if (! auth()->check()) {
            return false;
        }
        $user = auth()->user();

        if ($user->hasRole('Developer') || $user->can("manage {$menu}")) {
            return true;
        }

        // Check granular permissions (both direct and role-based)
        return $user->getAllPermissions()
            ->contains(fn ($p) => str_starts_with($p->name, "manage {$menu}:"));
    }
}

if (! function_exists('get_setting')) {
    function get_setting(string $key, mixed $default = null): mixed
    {
        return app(SystemConfigService::class)->get($key, $default);
    }
}

if (! function_exists('set_setting')) {
    function set_setting(string $key, mixed $value): bool
    {
        $request = app()->bound('request') ? request() : null;

        app(SystemConfigService::class)->update([$key => $value], [], Auth::id(), $request);

        return true;
    }
}

if (! function_exists('format_date')) {
    function format_date($date): string
    {
        if (! $date) {
            return '-';
        }
        $format = get_setting('regional_date_format', 'd/m/Y');

        return Carbon::parse($date)->format($format);
    }
}

if (! function_exists('format_time')) {
    function format_time($time): string
    {
        if (! $time) {
            return '-';
        }
        $format = get_setting('regional_time_format', 'H:i');

        return Carbon::parse($time)->format($format);
    }
}

if (! function_exists('format_datetime')) {
    function format_datetime($datetime): string
    {
        if (! $datetime) {
            return '-';
        }
        $dateFormat = get_setting('regional_date_format', 'd/m/Y');
        $timeFormat = get_setting('regional_time_format', 'H:i');

        return Carbon::parse($datetime)->format("{$dateFormat} {$timeFormat}");
    }
}
