<?php
declare(strict_types=1);

function employee_management_page_url(string $page, array $params = array()): string
{
    $allowedPages = array(
        'employees.php',
        'positions.php',
    );

    if (!in_array($page, $allowedPages, true)) {
        $page = 'employees.php';
    }

    $filteredParams = array();
    foreach ($params as $key => $value) {
        if ($value === null || $value === '') {
            continue;
        }

        $filteredParams[$key] = $value;
    }

    if (empty($filteredParams)) {
        return $page;
    }

    return $page . '?' . http_build_query($filteredParams);
}

function render_employee_management_tabs(string $activePage): string
{
    $items = array(
        array('employees.php', 'employees', ui_text('จัดการพนักงาน', 'Employee management')),
        array('positions.php', 'positions', ui_text('จัดการตำแหน่งงาน', 'Position management')),
    );

    $links = array();
    foreach ($items as $item) {
        $href = $item[0];
        $pageKey = $item[1];
        $label = $item[2];
        $class = $activePage === $pageKey ? 'button button-accent' : 'button button-secondary';
        $links[] = '<a class="' . e($class) . '" href="' . e($href) . '">' . e($label) . '</a>';
    }

    return '<section class="service-management-tabs">' . implode('', $links) . '</section>';
}

function employee_branch_scope_options(): array
{
    return array(
        'assigned' => ui_text('สาขาที่กำหนด', 'Assigned branches'),
        'all' => ui_text('ทุกสาขา', 'All branches'),
    );
}

function employee_branch_scope_label(string $scope): string
{
    $options = employee_branch_scope_options();

    return $options[$scope] ?? $scope;
}

function employee_status_label(bool $isActive): string
{
    return $isActive
        ? ui_text('ใช้งาน', 'Active')
        : ui_text('ปิดใช้งาน', 'Inactive');
}

function employee_permission_group_label(string $permissionKey): string
{
    if (str_starts_with($permissionKey, 'booking')) {
        return ui_text('เปิดบิล', 'Booking');
    }

    if (str_starts_with($permissionKey, 'quick_service')) {
        return ui_text('ควิกเซอร์วิส', 'Quick Service');
    }

    if (str_starts_with($permissionKey, 'queue')) {
        return ui_text('คิวงาน', 'Queue');
    }

    if ($permissionKey === 'dashboard') {
        return ui_text('แดชบอร์ด', 'Dashboard');
    }

    if ($permissionKey === 'services') {
        return ui_text('บริการ', 'Services');
    }

    if ($permissionKey === 'member') {
        return ui_text('สมาชิก', 'Member');
    }

    if ($permissionKey === 'loyalty') {
        return ui_text('สะสมแต้ม', 'Loyalty');
    }

    if ($permissionKey === 'employees') {
        return ui_text('พนักงาน', 'Employees');
    }

    if (str_contains($permissionKey, 'report') || str_contains($permissionKey, 'sale') || str_contains($permissionKey, 'void')) {
        return ui_text('รายงาน', 'Reports');
    }

    return ui_text('ทั่วไป', 'General');
}
