<?php

namespace App\Modules\HR\Organization\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Exists;

final class OrganizationRequestRules
{
    /** @return array<string,mixed> */
    public static function for(string $entity): array
    {
        $base = ['code' => ['required', 'string', 'max:64'], 'name' => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string', 'max:500'], 'is_active' => ['sometimes', 'boolean']];

        return array_merge($base, match ($entity) {
            'Section' => ['department_id' => ['required', 'integer', self::tenantExists('departments')]],
            'Team' => ['section_id' => ['nullable', 'integer', self::tenantExists('hr_sections')], 'department_id' => ['nullable', 'integer', self::tenantExists('departments')]],
            'JobGrade' => ['level' => ['required', 'integer', 'min:1'], 'min_salary' => ['nullable', 'numeric', 'min:0'], 'max_salary' => ['nullable', 'numeric', 'gte:min_salary']],
            'WorkLocation' => ['branch_id' => ['nullable', 'integer', self::tenantExists('hr_branches')], 'address' => ['nullable', 'string', 'max:255']],
            default => [],
        });
    }

    private static function tenantExists(string $table): Exists
    {
        return Rule::exists($table, 'id')->where('tenant_id', auth()->user()?->tenant_id);
    }
}
