<?php

namespace Modules\SystemCore\App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user()->can('systemcore.edit-product');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, array<int, string>>
     */
    public function rules(): array
    {
        $productId = $this->route('product');

        return [
            'name' => ['required', 'string', 'max:255'],
            'product_code' => ['nullable', 'string', 'max:255', 'unique:core_products,product_code,'.$productId],
            'sku' => ['nullable', 'string', 'max:255', 'unique:core_products,sku,'.$productId],
            'barcode' => ['nullable', 'string', 'max:255', 'unique:core_products,barcode,'.$productId],
            'category_id' => ['nullable', 'exists:core_product_categories,id'],
            'brand' => ['nullable', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'base_price' => ['required', 'numeric', 'min:0'],
            'cost_price' => ['nullable', 'numeric', 'min:0'],
            'selling_price' => ['required', 'numeric', 'min:0'],
            'tax_id' => ['nullable', 'exists:core_tax_configurations,id'],
            'unit_of_measure' => ['nullable', 'string', 'max:50'],
            'weight' => ['nullable', 'numeric', 'min:0'],
            'weight_unit' => ['nullable', 'string', 'max:20'],
            'min_stock_level' => ['nullable', 'numeric', 'min:0'],
            'max_stock_level' => ['nullable', 'numeric', 'min:0'],
            'reorder_level' => ['nullable', 'numeric', 'min:0'],
            'thumbnail' => ['nullable', 'string'],
            'is_trackable' => ['boolean'],
            'status' => ['required', 'string', 'in:active,inactive,discontinued'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.required' => __('Product name is required.'),
            'name.max' => __('Product name cannot exceed :max characters.'),
            'sku.unique' => __('This SKU is already in use.'),
            'barcode.unique' => __('This barcode is already in use.'),
            'category_id.exists' => __('The selected category does not exist.'),
            'tax_id.exists' => __('The selected tax configuration does not exist.'),
            'base_price.required' => __('Base price is required.'),
            'base_price.min' => __('Base price must be at least :min.'),
            'selling_price.required' => __('Selling price is required.'),
            'selling_price.min' => __('Selling price must be at least :min.'),
            'status.required' => __('Status is required.'),
            'status.in' => __('Invalid status value.'),
            'product_code.unique' => __('This product code is already in use.'),
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'name' => __('product name'),
            'sku' => __('SKU'),
            'barcode' => __('barcode'),
            'category_id' => __('category'),
            'brand' => __('brand'),
            'description' => __('description'),
            'base_price' => __('base price'),
            'cost_price' => __('cost price'),
            'selling_price' => __('selling price'),
            'tax_id' => __('tax configuration'),
            'unit_of_measure' => __('unit of measure'),
            'weight' => __('weight'),
            'weight_unit' => __('weight unit'),
            'min_stock_level' => __('minimum stock level'),
            'max_stock_level' => __('maximum stock level'),
            'reorder_level' => __('reorder level'),
            'status' => __('status'),
            'product_code' => __('product code'),
        ];
    }
}
