<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Exceptions\ValidationException;

class StorePaymentRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'client_id' => 'required|exists:clients,id',
            'amount' => 'required|numeric|min:1|max:10000000',
            'payment_date' => 'required|date|before_or_equal:today',
            'payment_method' => 'required|in:cash,cheque,bank_transfer,mobile_money',
            'reference_number' => 'nullable|string|max:100|unique:transactions',
            'description' => 'required|string|min:5|max:500',
            'loan_id' => 'nullable|exists:loans,id',
            'savings_account_id' => 'nullable|exists:savings_accounts,id',
            'cheque_number' => 'nullable|string|max:50',
            'bank_name' => 'nullable|string|max:255',
            'account_number' => 'nullable|string|max:50',
            'transaction_id' => 'nullable|string|max:100',
            'notes' => 'nullable|string|max:1000',
            'attachments' => 'nullable|array|max:3',
            'attachments.*' => 'file|max:2048|mimes:pdf,jpeg,png,jpg',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'client_id.required' => 'Please select a client.',
            'client_id.exists' => 'Selected client does not exist.',
            'amount.required' => 'Please enter the payment amount.',
            'amount.numeric' => 'Payment amount must be a valid number.',
            'amount.min' => 'Payment amount must be at least KSh 1.',
            'amount.max' => 'Payment amount cannot exceed KSh 10,000,000.',
            'payment_date.required' => 'Please specify the payment date.',
            'payment_date.date' => 'Payment date must be a valid date.',
            'payment_date.before_or_equal' => 'Payment date cannot be in the future.',
            'payment_method.required' => 'Please select the payment method.',
            'payment_method.in' => 'Payment method must be cash, cheque, bank transfer, or mobile money.',
            'reference_number.max' => 'Reference number cannot exceed 100 characters.',
            'reference_number.unique' => 'This reference number is already in use.',
            'description.required' => 'Please provide a description for this payment.',
            'description.min' => 'Description must be at least 5 characters.',
            'description.max' => 'Description cannot exceed 500 characters.',
            'loan_id.exists' => 'Selected loan does not exist.',
            'savings_account_id.exists' => 'Selected savings account does not exist.',
            'cheque_number.max' => 'Cheque number cannot exceed 50 characters.',
            'bank_name.max' => 'Bank name cannot exceed 255 characters.',
            'account_number.max' => 'Account number cannot exceed 50 characters.',
            'transaction_id.max' => 'Transaction ID cannot exceed 100 characters.',
            'notes.max' => 'Notes cannot exceed 1000 characters.',
            'attachments.array' => 'Attachments must be provided as an array.',
            'attachments.max' => 'Maximum 3 attachments allowed.',
            'attachments.*.file' => 'Each attachment must be a file.',
            'attachments.*.max' => 'Each attachment cannot exceed 2MB.',
            'attachments.*.mimes' => 'Attachments must be PDF, JPEG, PNG, or JPG files.',
        ];
    }

    /**
     * Handle a failed validation attempt.
     */
    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    {
        throw new ValidationException(
            'Payment validation failed. Please check your input and try again.',
            'PAYMENT_VALIDATION_FAILED',
            $validator->errors()->toArray()
        );
    }
}
