<?php

namespace App\Http\Requests\Admin\Users;

use App\Enums\Gender;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AdministratorRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can($this->isMethod('post') ? 'create-administrators' : 'update-administrators');
    }

    public function rules(): array
    {
        $userId = $this->route('user')?->id;

        return [
            'username' => [
                'required',
                'string',
                'max:20',
                'min:5',
                'alpha_dash',
                $userId === null
                    ? Rule::unique('users')
                    : Rule::unique('users')->ignore($userId),
            ],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                $userId === null
                    ? Rule::unique('users')
                    : Rule::unique('users')->ignore($userId),
            ],
            'role' => ['required', 'string', Rule::in(['staff-admin', 'staff-keuangan'])],

            // Profile
            'full_name' => ['required', 'string', 'max:150'],
            'phone_number' => ['required', 'string', 'max:20'],
            'address' => ['required', 'string'],
            'gender' => ['required', 'string', Rule::in(Gender::values())],
            'birth_date' => ['required', 'date'],
            'birth_place' => ['required', 'string', 'max:100'],
        ];
    }
}
