<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PaymentSplitRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $rules = [
            'payment_id'  => ['sometimes', 'integer', 'exists:payments,id'],
            'provider_id' => ['sometimes', 'integer', 'exists:providers,id'],

            'gateway_provider'                  => ['sometimes', 'string', 'max:255'],
            'gateway_transfer_target_reference' => ['nullable', 'string', 'max:255'],
            'gateway_transfer_target_label'     => ['nullable', 'string', 'max:255'],

            'status' => ['sometimes', 'in:pending,processing,transferred,failed,cancelled'],

            'gross_amount'       => ['sometimes', 'numeric', 'min:0'],
            'gateway_fee_amount' => ['sometimes', 'numeric', 'min:0'],
            'net_amount'         => ['sometimes', 'numeric', 'min:0'],

            'transferred_at' => ['nullable', 'date'],

            'metadata' => ['nullable', 'array'],
        ];

        if ($this->isMethod('POST')) {
            $rules['payment_id']   = ['required', 'integer', 'exists:payments,id'];
            $rules['provider_id']  = ['required', 'integer', 'exists:providers,id'];
            $rules['gross_amount'] = ['required', 'numeric', 'min:0'];
            $rules['net_amount']   = ['required', 'numeric', 'min:0'];
        }

        return $rules;
    }
}
