<?php

declare(strict_types=1);

namespace App\Http\Requests\Employee;

use Illuminate\Foundation\Http\FormRequest;

/**
 * Rules shared by the store and update employee requests.
 */
final class EmployeeRules
{
    /**
     * @return array<string, mixed>
     */
    public static function normalise(FormRequest $request): array
    {
        $teamId = $request->input('team_id');
        $locationId = $request->input('location_id');

        return [
            'employee_code' => mb_strtoupper(trim((string) $request->input('employee_code'))),
            'first_name' => trim((string) $request->input('first_name')),
            'last_name' => self::nullableString($request->input('last_name')),
            'email' => self::nullableString($request->input('email')),
            'personal_email' => self::nullableString($request->input('personal_email')),
            'phone' => self::nullableString($request->input('phone')),
            'team_id' => ($teamId === null || $teamId === '') ? null : (int) $teamId,
            'designation' => self::nullableString($request->input('designation')),
            'squad' => self::nullableString($request->input('squad')),
            'location_id' => ($locationId === null || $locationId === '') ? null : (int) $locationId,
            'status' => trim((string) $request->input('status')),
            'date_of_joining' => self::nullableString($request->input('date_of_joining')),
            'date_of_exit' => self::nullableString($request->input('date_of_exit')),
            'notes' => self::nullableString($request->input('notes')),
        ];
    }

    private static function nullableString(mixed $value): ?string
    {
        $value = trim((string) $value);

        return $value === '' ? null : $value;
    }
}
