<?php

namespace Modules\Inventory\Http\Requests\Product;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Core\Enums\AmountTypeEnum;
use Modules\Core\Enums\PriceTypeEnum;
use Modules\Core\Enums\VatTypeEnum;
use Modules\Core\Traits\FailedValidation;
use Modules\Core\Enums\StatusEnum;
use Modules\Inventory\Enums\ProductTypeEnum;

class StoreProductRequest extends FormRequest
{
    use FailedValidation;

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'sku' => [
                'nullable',
                'string',
                'max:100',
                Rule::unique('products', 'sku')
            ],
            'barcode' => ['nullable', 'string', 'max:100'],
            'image' => ['nullable', 'image', 'mimes:jpg,jpeg,png', 'max:2048'],
            'category_id' => ['required', 'exists:categories,id'],
            'base_unit_id' => ['required', 'exists:units,id'],
            'product_type' => [
                'required',
                Rule::in(ProductTypeEnum::casesAsArray())
            ],
            'mrp' => ['nullable', 'numeric', 'min:0'],
            'default_selling_price' => ['nullable', 'numeric', 'min:0'],
            'vat_price_type' => ['required', Rule::in(PriceTypeEnum::casesAsArray())],
            'vat_type' => ['required', Rule::in(VatTypeEnum::casesAsArray())],
            'vat_rate' => ['nullable', 'numeric', 'min:0', 'max:100'],
            'subsidy_type' => [
                'required',
                Rule::in(AmountTypeEnum::casesAsArray())
            ],
            'subsidy_value' => ['nullable', 'numeric', 'min:0'],
            'has_unit_conversion' => ['boolean'],
            'alert_qty' => ['nullable', 'integer', 'min:0'],
            'shelf_no' => ['nullable', 'string', 'max:100'],
            'status' => [
                'required',
                Rule::in(StatusEnum::casesAsArray())
            ],
        ];
    }

    protected function prepareForValidation()
    {
        $this->merge([
            'has_unit_conversion' => $this->boolean('has_unit_conversion'),
            'mrp' => $this->filled('mrp') ? (float) $this->mrp : 0,
            'default_selling_price' => $this->filled('default_selling_price') ? (float) $this->default_selling_price : 0,
            'subsidy_value' => $this->filled('subsidy_value') ? (float) $this->subsidy_value : 0,
            'vat_rate' => $this->filled('vat_rate') ? (float) $this->vat_rate : 0,
            'alert_qty' => $this->filled('alert_qty') ? (int) $this->alert_qty : 0,
        ]);
    }

    public function attributes(): array
    {
        return [
            'base_unit_id' => 'unit',
            'inventory_department_id' => 'department',
        ];
    }

    public function messages(): array
    {
        return [
            'sku.unique' => 'This SKU is already assigned to another product.',
            'product_type.in' => 'The selected product type is invalid. Allowed types are: ' . implode(', ', ProductTypeEnum::casesAsArray()),
            'subsidy_type.in' => 'The selected subsidy type is invalid. Allowed types are: ' . implode(', ', AmountTypeEnum::casesAsArray()),
            'inventory_department_id.exists' =>
            'The selected department is invalid.',
            'category_id.exists' =>
            'The selected category does not exist.',
            'base_unit_id.exists' =>
            'The selected unit is invalid.',
        ];
    }

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