<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PurchaseInvoiceRequest 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
    {
        $id = $this->route('purchase_invoice') ? $this->route('purchase_invoice') : null;
        $isEdit = !is_null($id);

        $requiredDateRule = 'required|date|after_or_equal:today';

        if ($isEdit) {
            $invoice = \App\Models\Invoice::find($id);
            if ($invoice) {
                $requiredDateRule = 'required|date|after_or_equal:' . $invoice->created_at->format('Y-m-d');
            }
        }

        return [
            'type' => 'required|string|in:purchase',
            'prefix' => 'required|string|size:2',
            'company' => 'required|exists:companies,id',
            'party' => 'nullable|required_if:is_approved,1|exists:parties,id',
            'code' => 'required|string|max:255|unique:invoices,code,' . $id . ',id,company_id,' . $this->company,
            'required_date' => $requiredDateRule,
            'payment_deadline' => 'nullable|date|after:today',
            'is_urgent' => 'required|in:0,1',
            'invoice_items' => 'required|array|min:1',
            'invoice_items.*.code' => 'nullable|string|max:255',
            'invoice_items.*.raw_material_item' => 'required|string|exists:raw_material_items,id',
            'invoice_items.*.balance' => 'nullable',
            'invoice_items.*.min_level' => 'nullable',
            'invoice_items.*.max_level' => 'nullable',
            'invoice_items.*.title' => 'nullable|string',
            'invoice_items.*.description' => 'nullable|string',
            'invoice_items.*.quantity' => 'required|integer|min:1',
            'invoice_items.*.unit_price' => 'required_if:is_approved,1|nullable|numeric|min:0',
            'invoice_items.*.unit' => 'required|string',
            'invoice_items.*.lp_ref' => 'nullable|string|max:255',
            'invoice_items.*.lp_price' => 'nullable',
            'invoice_items.*.ip_po_list' => 'nullable|string|max:255',
            'invoice_items.*.ip_quantity' => 'nullable',
            'invoice_items.*.consumption_6_month' => 'nullable',
            // 'invoice_items.*.tax_rate' => 'nullable|numeric|min:0',
            // 'invoice_items.*.discount_type' => 'required|string|in:percent,flat',
            // 'invoice_items.*.discount' => 'nullable|numeric|min:0',
            'invoice_items.*.total_amount' => 'required|numeric|min:0',
            // 'items_cost_without_tax' => 'nullable|numeric|min:0',
            // 'invoice_tax_rate' => 'nullable|numeric|min:0',
            // 'total_invoice_tax' => 'nullable|numeric|min:0',
            // 'items_cost_with_tax' => 'nullable|numeric|min:0',
            // 'discount_type' => 'nullable|string|in:percent,flat',
            // 'discount' => 'nullable|numeric|min:0',			
            'total_amount' => 'required|numeric|min:0',
            'note' => 'nullable|string|max:1000',
            // 'status' => 'nullable|in:pending,completed,partial', it was giving error due to case sensitivity
            'status' => 'nullable',
            'is_approved' => 'nullable|boolean',
        ];
    }

    public function messages(): array
    {
        return [
            'type.required' => 'Invoice type is required.',
            'type.in' => 'Invalid invoice type selected.',

            'prefix.required' => 'Prefix is required.',
            'prefix.size' => 'Prefix must be exactly 2 characters.',

            'company.required' => 'Please select a company.',
            'company.exists' => 'Selected company does not exist.',

            'party.required_if' => 'Party is required when the invoice is approved.',
            'party.exists' => 'Selected party does not exist.',

            'code.required' => 'Invoice code is required.',
            'code.unique' => 'This invoice code already exists for the selected company.',
            'code.max' => 'Invoice code must not exceed 255 characters.',

            'required_date.required' => 'Required date is mandatory.',
            'required_date.date' => 'Please enter a valid date.',
            'required_date.after_or_equal' => 'Required date cannot be earlier than today.',

            'payment_deadline.date' => 'Please enter a valid payment deadline.',
            'payment_deadline.after' => 'Payment deadline must be a future date.',

            'is_urgent.required' => 'Please specify if this invoice is urgent.',

            'invoice_items.required' => 'Please add at least one invoice item.',
            'invoice_items.array' => 'Invoice items must be a valid list.',

            'invoice_items.*.raw_material_item.required' => 'Raw material item is required for each row.',
            'invoice_items.*.raw_material_item.exists' => 'Selected raw material item does not exist.',

            'invoice_items.*.quantity.required' => 'Quantity is required for each item row.',
            'invoice_items.*.quantity.integer' => 'Quantity must be a whole number.',
            'invoice_items.*.quantity.min' => 'Quantity must be at least 1.',

            'invoice_items.*.unit_price.required_if' => 'Unit price is required when the invoice is approved.',
            'invoice_items.*.unit_price.numeric' => 'Unit price must be a valid number.',
            'invoice_items.*.unit_price.min' => 'Unit price cannot be negative.',

            'invoice_items.*.unit.required' => 'Unit is required for each item row.',

            'invoice_items.*.total_amount.required' => 'Total amount is required for each item.',
            'invoice_items.*.total_amount.numeric' => 'Total amount must be a valid number.',
            'invoice_items.*.total_amount.min' => 'Total amount must be at least 0.',

            'total_amount.required' => 'Total invoice amount is required.',
            'total_amount.numeric' => 'Total amount must be a valid number.',
            'total_amount.min' => 'Total amount cannot be negative.',

            'note.max' => 'Note cannot exceed 1000 characters.',

            'is_approved.boolean' => 'Approval value must be true or false.',
        ];
    }
}
