<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RejectionReturnNoteRequest 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
    {
        $rejectionReturnNoteId = $this->rejectionReturnNote ? $this->rejectionReturnNote->id : null;

        return [
            'code' => 'required|string|max:255|unique:goods_receivings,code,' . $rejectionReturnNoteId . ',id,company_id,' . $this->company_id,
            'supplier' => 'required|exists:parties,id',
            'invoice' => 'required|exists:invoices,id',
            'is_general' => 'nullable|boolean',
            'is_production' => 'nullable|boolean',
            // 'supplier_bill_no' => 'required|string|max:255',
            'store_issue_requisition' => 'required|exists:store_issue_requisitions,id',
            'is_approved' => 'nullable|boolean',
            'company' => 'required|exists:companies,id',
            'type' => 'required|in:gate_pass,burn_note',
            'items' => 'required|array|min:1',
            'items.*.product' => 'nullable|required_if:items.*.types,product|exists:products,id',
            'items.*.raw_material_item' => 'nullable|required_if:items.*.types,raw_material_item|exists:raw_material_items,id',
            'items.*.quantity' => 'required|numeric|min:1',
            'items.*.unit_price' => 'required|numeric|min:0',
            'items.*.unit' => 'required|string|max:50',
            'items.*.total' => 'required|numeric|min:0',
            'items.*.remarks' => 'required',
            'items.*.name' => 'required',
        ];
    } 

    public function messages()
    {
        return [
            'code.required' => 'Code is required.',
            'supplier.required' => 'Supplier is required.',
            'invoice.required' => 'Purchase is required.',
            // 'supplier_bill_no.required' => 'Supplier Bill No. is required.',
            'store_issue_requisition.required' => 'Store Issue Requisition is required.',
            'company.required' => 'Company is required.',
            'type.required' => 'Type is required.',
            'items.required' => 'Items are required.',
            'items.*.product.required_if' => 'Product is required.',
            'items.*.raw_material_item.required_if' => 'Raw Material Item is required.',
            'items.*.quantity.required' => 'Quantity is required.',
            'items.*.unit_price.required' => 'Unit Price is required.',
            'items.*.unit.required' => 'Unit is required.',
            'items.*.total.required' => 'Total is required.',
            'items.*.remarks.required' => 'Remarks is required.',
            'items.*.name.required' => 'Description is required.',
        ];
    }
}
