<?php

namespace App\Domains\Catalog\Requests;

use App\Domains\Catalog\Enums\CatalogItemType;
use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Enums\InventorySubject;
use App\Domains\Shared\Rules\ImageOrBase64Rule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreCatalogItemRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    /** @return array<string, mixed> */
    public function rules(): array
    {
        $tenantCode = $this->route('tenant')?->codigo;
        $type = $this->input('type', CatalogItemType::Standard->value);
        $isBundle = $type === CatalogItemType::Bundle->value;

        return [
            'tenant_code' => ['prohibited'],
            'type' => ['sometimes', Rule::enum(CatalogItemType::class)],
            'category_id' => [
                'sometimes',
                'nullable',
                'integer',
                Rule::exists('categorias', 'id')->where(
                    fn ($query) => $query->where('tenant_code', $tenantCode)
                ),
            ],
            'brand_id' => [
                'sometimes',
                'nullable',
                'integer',
                Rule::exists('brands', 'id')->where(
                    fn ($query) => $query->where('tenant_codigo', $tenantCode)
                ),
            ],
            'slug' => [
                'required',
                'string',
                'max:255',
                Rule::unique('catalog_items', 'slug')->where(
                    fn ($query) => $query->where('tenant_code', $tenantCode)
                ),
            ],
            'nombre' => ['required', 'string', 'max:255'],
            'group_order' => ['sometimes', 'integer', 'min:0'],
            'descripcion' => ['sometimes', 'nullable', 'string'],
            'precio' => ['required', 'numeric', 'min:0'],
            'inventory_policy' => [Rule::prohibitedIf($isBundle), 'sometimes', Rule::enum(InventoryPolicy::class)],
            'inventory_subject' => ['sometimes', Rule::enum(InventorySubject::class)],
            'max_units_per_user' => ['sometimes', 'nullable', 'integer', 'min:1'],
            'has_tickets' => [Rule::prohibitedIf($isBundle), 'sometimes', 'boolean'],
            'real_stock' => [Rule::prohibitedIf($isBundle), 'sometimes', 'integer', 'min:0'],
            'inventory_id' => ['prohibited'],
            'reserved_stock' => ['prohibited'],
            'sold_units' => ['prohibited'],
            'attribute_codes' => [Rule::prohibitedIf($isBundle), 'sometimes', 'array'],
            'attribute_codes.*' => [
                'required',
                'string',
                'distinct',
                Rule::exists('attribute', 'codigo')->where(
                    fn ($query) => $query->where('tenant_codigo', $tenantCode)
                ),
            ],
            'multi_select_attribute_codes' => [Rule::prohibitedIf($isBundle), 'sometimes', 'array'],
            'multi_select_attribute_codes.*' => [
                'required',
                'string',
                'distinct',
                Rule::exists('attribute', 'codigo')->where(
                    fn ($query) => $query->where('tenant_codigo', $tenantCode)
                ),
            ],
            'hidden_attribute_codes' => [Rule::prohibitedIf($isBundle), 'sometimes', 'array'],
            'hidden_attribute_codes.*' => [
                'required',
                'string',
                'distinct',
                Rule::exists('attribute', 'codigo')->where(
                    fn ($query) => $query->where('tenant_codigo', $tenantCode)
                ),
            ],
            'images' => ['sometimes', 'array'],
            'images.*' => ['required', new ImageOrBase64Rule],
            'variants' => [Rule::prohibitedIf($isBundle), 'sometimes', 'array'],
            'variants.*.real_stock' => ['sometimes', 'integer', 'min:0'],
            'variants.*.descripcion' => ['sometimes', 'nullable', 'string'],
            'variants.*.precio' => ['sometimes', 'nullable', 'numeric', 'min:0', 'max:99999999.99'],
            'variants.*.event_date_id' => [
                'sometimes',
                'nullable',
                'integer',
                Rule::exists('event_dates', 'id')->where(
                    fn ($query) => $query->where('tenant_code', $tenantCode)
                ),
            ],
            'variants.*.event_date_ids' => ['sometimes', 'array', 'min:1'],
            'variants.*.event_date_ids.*' => [
                'required',
                'integer',
                'distinct',
                Rule::exists('event_dates', 'id')->where(
                    fn ($query) => $query->where('tenant_code', $tenantCode)
                ),
            ],
            'variants.*.inventory_id' => ['prohibited'],
            'variants.*.reserved_stock' => ['prohibited'],
            'variants.*.sold_units' => ['prohibited'],
            'variants.*.values' => ['sometimes', 'array'],
            'variants.*.values.*' => ['nullable'],
            'variants.*.values.*.*' => ['required', 'string'],
            'variants.*.images' => ['sometimes', 'array'],
            'variants.*.images.*' => ['required', new ImageOrBase64Rule],
            'components' => [
                Rule::requiredIf($isBundle),
                Rule::prohibitedIf(! $isBundle),
                'array',
                'min:1',
            ],
            'components.*.catalog_item_id' => [
                'required',
                'integer',
                Rule::exists('catalog_items', 'id')->where(
                    fn ($query) => $query->where('tenant_code', $tenantCode)
                ),
            ],
            'components.*.variant_id' => [
                'sometimes',
                'nullable',
                'integer',
                Rule::exists('variantes', 'id'),
            ],
            'components.*.quantity' => ['required', 'integer', 'min:1'],
        ];
    }
}
