<?php

namespace App\Http\Requests\Supplier;

use Illuminate\Foundation\Http\FormRequest;

class UpdateSupplierRequest 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
    {
        $supplierId = $this->route('supplier')->id;

        return [
            'name' => 'required|string|max:255',
            'email' => 'nullable|email|max:255',
            'phone' => 'required|string|max:20',
            'address' => 'nullable|string|max:500',
            'tax_number' => 'nullable|string|max:50',
            'opening_balance' => 'nullable|numeric|min:-999999999.9999|max:999999999.9999',
            'pay_term_no' => 'nullable|integer|min:1|max:999',
            'pay_term_type' => 'nullable|in:days,month',
            'supplier_code' => 'nullable|string|max:50|unique:accounts,supplier_code,' . $supplierId,
            'contact_person' => 'nullable|string|max:255',
            'credit_rating' => 'nullable|numeric|min:0|max:10',
            'is_active' => 'nullable|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Supplier name is required.',
            'name.max' => 'Supplier name must not exceed 255 characters.',
            'email.email' => 'Please provide a valid email address.',
            'email.max' => 'Email must not exceed 255 characters.',
            'phone.max' => 'Phone number must not exceed 20 characters.',
            'address.max' => 'Address must not exceed 500 characters.',
            'tax_number.max' => 'Tax number must not exceed 50 characters.',
            'opening_balance.numeric' => 'Opening balance must be a valid number.',
            'opening_balance.min' => 'Opening balance must be at least -999,999,999.9999.',
            'opening_balance.max' => 'Opening balance must not exceed 999,999,999.9999.',
            'pay_term_no.integer' => 'Payment term number must be a whole number.',
            'pay_term_no.min' => 'Payment term number must be at least 1.',
            'pay_term_no.max' => 'Payment term number must not exceed 999.',
            'pay_term_type.in' => 'Payment term type must be either "days" or "month".',
            'credit_limit.numeric' => 'Credit limit must be a valid number.',
            'credit_limit.min' => 'Credit limit must be at least 0.',
            'credit_limit.max' => 'Credit limit must not exceed 999,999,999.9999.',
            'supplier_code.unique' => 'This supplier code is already in use.',
            'supplier_code.max' => 'Supplier code must not exceed 50 characters.',
            'contact_person.max' => 'Contact person name must not exceed 255 characters.',
            'payment_terms.max' => 'Payment terms must not exceed 255 characters.',
            'credit_rating.numeric' => 'Credit rating must be a valid number.',
            'credit_rating.min' => 'Credit rating must be at least 0.',
            'credit_rating.max' => 'Credit rating must not exceed 10.',
            'notes.max' => 'Notes must not exceed 1000 characters.',
            'is_active.boolean' => 'Active status must be true or false.',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     */
    public function attributes(): array
    {
        return [
            'name' => 'Supplier Name',
            'email' => 'Email Address',
            'phone' => 'Phone Number',
            'address' => 'Address',
            'tax_number' => 'Tax Number',
            'opening_balance' => 'Opening Balance',
            'pay_term_no' => 'Payment Term Number',
            'pay_term_type' => 'Payment Term Type',
            'credit_limit' => 'Credit Limit',
            'supplier_code' => 'Supplier Code',
            'contact_person' => 'Contact Person',
            'payment_terms' => 'Payment Terms',
            'credit_rating' => 'Credit Rating',
            'notes' => 'Notes',
            'is_active' => 'Active Status',
        ];
    }
}
