<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class LoanProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth()->user()->can('create loan products') || auth()->user()->can('edit loan products');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'name' => 'required|string|max:191|unique:loan_products,name',
            'description' => 'nullable|string|max:5000',
            'terms_and_conditions' => 'nullable|string|max:10000',
            'interest_type' => 'required|in:fixed,reducing_balance,flat_rate',
            'interest_rate' => 'required|numeric|min:0|max:100',
            'interest_rate_display' => 'nullable|string|max:50',
            'min_amount' => 'required|numeric|min:0',
            'max_amount' => 'required|numeric|gt:min_amount',
            'default_amount' => 'nullable|numeric|min:0',
            'currency' => 'required|in:USD,SSP,KES,UGX',
            'min_term_months' => 'required|integer|min:1',
            'max_term_months' => 'required|integer|gte:min_term_months',
            'default_term_months' => 'nullable|integer|min:1',
            'processing_fee' => 'nullable|numeric|min:0',
            'processing_fee_percentage' => 'nullable|numeric|min:0|max:100',
            'late_payment_penalty' => 'nullable|numeric|min:0',
            'is_active' => 'nullable|boolean',
            'is_featured' => 'nullable|boolean',
            'requires_collateral' => 'nullable|boolean',
            'requires_guarantor' => 'nullable|boolean',
            'min_guarantors' => 'nullable|integer|min:0',
            'min_membership_months' => 'nullable|integer|min:0',
            'min_contribution_balance' => 'nullable|numeric|min:0',
            'theme_color' => 'nullable|string|max:50',
            'icon' => 'nullable|string|max:100',
            'eligible_categories' => 'nullable|array',
        ];

        // For update, make name unique except for current product
        if ($this->isMethod('put') && $this->loanProduct) {
            $rules['name'] = 'required|string|max:191|unique:loan_products,name,' . $this->loanProduct->id;
        }

        return $rules;
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Product name is required.',
            'name.unique' => 'A product with this name already exists.',
            'interest_rate.required' => 'Interest rate is required.',
            'min_amount.required' => 'Minimum amount is required.',
            'max_amount.required' => 'Maximum amount is required.',
            'max_amount.gt' => 'Maximum amount must be greater than minimum amount.',
            'max_term_months.gte' => 'Maximum term must be greater than or equal to minimum term.',
        ];
    }
}
