<?php

namespace App\Http\Requests;

use App\Models\LaundrySaleProduct;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreLaundrySaleProductRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', \App\Models\LaundrySaleProduct::class) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'laundry_sale_category_id' => ['required', 'integer', 'exists:laundry_sale_categories,id'],
            'code' => ['nullable', 'string', 'max:64', 'unique:laundry_sale_products,code'],
            'name' => ['required', 'string', 'max:255'],
            'tipo' => ['required', 'string', Rule::in([LaundrySaleProduct::TIPO_SERVICIO, LaundrySaleProduct::TIPO_PRODUCTO])],
            'description' => ['nullable', 'string'],
            'default_etapa_id' => ['nullable', 'integer', 'exists:etapas,id'],
            'default_load_type_id' => ['nullable', 'integer', 'exists:load_types,id'],
            'default_tipo_servicio_id' => ['nullable', 'integer', 'exists:tipos_servicio,id'],
            'billing_unit' => ['required', 'string', Rule::in([LaundrySaleProduct::BILLING_UNIT_PIECE, LaundrySaleProduct::BILLING_UNIT_KG])],
            'unit_price' => ['required', 'numeric', 'min:0'],
            'extra_cost' => ['nullable', 'numeric', 'min:0'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['sometimes', 'boolean'],
            'inventory_product_id' => ['nullable', 'integer', 'exists:inventory_products,id'],
            'tracks_stock' => ['sometimes', 'boolean'],
            'stock_quantity' => ['nullable', 'integer', 'min:0', 'required_if:tracks_stock,true'],
            'service_prices' => ['nullable', 'array'],
            'service_prices.*.etapa_id' => ['required', 'integer', 'exists:etapas,id'],
            'service_prices.*.amount' => ['required', 'numeric', 'min:0'],
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function (\Illuminate\Validation\Validator $validator): void {
            $tipo = (string) $this->input('tipo', LaundrySaleProduct::TIPO_SERVICIO);
            if ($tipo === LaundrySaleProduct::TIPO_SERVICIO && $this->boolean('tracks_stock')) {
                $validator->errors()->add('tracks_stock', 'Solo los artículos tipo «producto» pueden controlar existencias.');
            }
            if ($this->boolean('tracks_stock') && $this->input('billing_unit') !== LaundrySaleProduct::BILLING_UNIT_PIECE) {
                $validator->errors()->add('tracks_stock', 'El inventario por piezas solo aplica con unidad de cobro «por pieza».');
            }

            $rows = $this->input('service_prices');
            if (! is_array($rows)) {
                return;
            }
            $seen = [];
            foreach ($rows as $row) {
                $id = isset($row['etapa_id']) ? (int) $row['etapa_id'] : 0;
                if ($id <= 0) {
                    continue;
                }
                if (isset($seen[$id])) {
                    $validator->errors()->add('service_prices', 'El tipo de servicio está duplicado en precios por servicio.');

                    return;
                }
                $seen[$id] = true;
            }
        });
    }
}
