@php
    use App\Modules\Inventory\Support\InventoryLifecycleState;
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('inventory.breadcrumb_root'), 'url' => route('inventory.items.index')],
        ['label' => $item->sku],
    ];
    $inactive = ! $item->is_active || $item->lifecycle_state !== InventoryLifecycleState::Active;
@endphp

<x-ui.layout.page
    :title="$item->name"
    :subtitle="$item->sku"
    page-icon="fe fe-box"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        <x-ui.buttons.secondary :href="route('inventory.items.index')" icon="fe fe-arrow-left">
            {{ __('common.back') }}
        </x-ui.buttons.secondary>
    </x-slot:actions>

    @if ($inactive)
        <div class="alert alert-warning d-flex align-items-center gap-2" role="alert">
            <span class="fe fe-alert-triangle"></span>
            <span>{{ __('inventory.items.inactive_warning') }}</span>
        </div>
    @endif

    <h5 class="mt-2">{{ __('inventory.items.hierarchy') }}</h5>
    <p class="small text-muted mb-1">{{ __('inventory.items.parent') }}: {{ $item->parent?->sku ?? '—' }}</p>

    <h5 class="mt-4">{{ __('inventory.items.variants') }}</h5>
    @if ($item->variants->isEmpty())
        <p class="text-muted small">{{ __('inventory.items.no_variants') }}</p>
    @else
        <ul class="small">
            @foreach ($item->variants as $v)
                <li>
                    <a href="{{ route('inventory.items.show', $v) }}">{{ $v->sku }}</a> — {{ $v->name }}
                </li>
            @endforeach
        </ul>
    @endif

    <h5 class="mt-4">{{ __('inventory.items.attributes') }}</h5>
    @if ($item->attributeValues->isEmpty())
        <p class="text-muted small">{{ __('inventory.items.no_attributes') }}</p>
    @else
        <x-ui.table.table :columns="[
            ['label' => 'Code'],
            ['label' => 'Type'],
            ['label' => 'Value'],
        ]">
            @foreach ($item->attributeValues as $val)
                <x-ui.table.row>
                    <td>{{ $val->attribute?->code }}</td>
                    <td>{{ $val->attribute?->type?->value ?? '—' }}</td>
                    <td>
                        {{ $val->value_text
                            ?? $val->value_textarea
                            ?? $val->value_number
                            ?? ($val->value_boolean !== null ? ($val->value_boolean ? __('common.yes') : __('common.no')) : null)
                            ?? $val->value_date
                            ?? ($val->value_json ? json_encode($val->value_json) : null)
                            ?? '—' }}
                    </td>
                </x-ui.table.row>
            @endforeach
        </x-ui.table.table>
    @endif

    <h5 class="mt-4">{{ __('inventory.items.pricing') }}</h5>
    @if ($item->prices->isEmpty())
        <p class="text-muted small">{{ __('inventory.items.no_prices') }}</p>
    @else
        <x-ui.table.table :columns="[
            ['label' => 'Price type'],
            ['label' => 'Amount'],
        ]">
            @foreach ($item->prices as $price)
                <x-ui.table.row>
                    <td>{{ $price->priceType?->code ?? '—' }}</td>
                    <td>{{ trim(($price->currency ?? '').' '.(string) ($price->amount ?? '')) ?: '—' }}</td>
                </x-ui.table.row>
            @endforeach
        </x-ui.table.table>
    @endif
</x-ui.layout.page>
