<?php

namespace App\Http\Requests;

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

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

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

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:150'],
            'contribution_type' => ['required', Rule::in(['daily', 'weekly', 'monthly', 'custom'])],
            'contribution_amount' => ['required', 'numeric', 'min:1', 'decimal:0,2'],
            'maximum_members' => ['required', 'integer', 'min:2', 'max:10000'],
            'start_date' => ['required', 'date'],
            'end_date' => ['nullable', 'date', 'after:start_date'],
            'status' => ['required', Rule::in(['draft', 'active', 'completed', 'cancelled'])],
            'member_ids' => ['array', 'max:10000'],
            'member_ids.*' => ['uuid', Rule::exists(Member::class, 'id')],
        ];
    }
}
