<?php

namespace App\Http\Requests\Admin\Master;

use App\Enums\Permission;
use App\Rules\PhoneNumber;
use Illuminate\Foundation\Http\FormRequest;

class SupplierRequest extends FormRequest
{
    public function authorize(): bool
    {
        $permission = $this->isMethod('POST')
            ? Permission::SUPPLIERS_CREATE
            : Permission::SUPPLIERS_UPDATE;

        return $this->user()?->can($permission->value) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:200'],
            'phone_number' => ['nullable', 'string', 'max:20', new PhoneNumber],
            'address' => ['nullable', 'string'],
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'name' => 'Nama',
            'phone_number' => 'Nomor Telepon',
            'address' => 'Alamat',
        ];
    }
}
