<?php

namespace App\Http\Requests;

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

class SaveContributionRequest extends FormRequest
{
    public function authorize(): bool
    {
        $item = $this->route('contribution');

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

    public function rules(): array
    {
        return [
            'ajo_group_id' => ['required', Rule::exists(AjoGroup::class, 'id')],
            'member_id' => ['required', Rule::exists(Member::class, 'id')],
            'amount_due' => ['required', 'numeric', 'min:0.01', 'decimal:0,2'],
            'amount_paid' => ['required', 'numeric', 'min:0', 'decimal:0,2', 'lte:amount_due'],
            'due_date' => ['required', 'date'],
            'payment_date' => ['nullable', 'date'],
            'payment_method' => ['nullable', Rule::in(['cash', 'transfer', 'card', 'ussd', 'other'])],
            'notes' => ['nullable', 'string', 'max:3000'],
        ];
    }
}
