<?php

namespace App\Http\Requests;

use App\Models\Member;
use App\Support\CurrentBusiness;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SaveMemberRequest extends FormRequest
{
    public function authorize(): bool
    {
        $member = $this->route('member');

        return $member ? $this->user()->can('update', $member) : $this->user()->can('create', Member::class);
    }

    public function rules(): array
    {
        return [
            'member_number' => ['required', 'string', 'max:50', Rule::unique('members')->where('business_id', app(CurrentBusiness::class)->id())->ignore($this->route('member'))],
            'full_name' => ['required', 'string', 'max:150'],
            'phone' => ['required', 'string', 'max:30'],
            'whatsapp' => ['nullable', 'string', 'max:30'],
            'email' => ['nullable', 'email', 'max:255'],
            'gender' => ['nullable', Rule::in(['male', 'female', 'other'])],
            'address' => ['nullable', 'string', 'max:1000'],
            'notes' => ['nullable', 'string', 'max:3000'],
            'is_active' => ['boolean'],
        ];
    }
}
