{# app/templates/store/inventory.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% from 'shared/macros/headers.html' import page_header_flex, refresh_button %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/modals.html' import modal_simple %}
{% from 'shared/macros/tables.html' import table_wrapper, table_header %}

{% block title %}Inventory{% endblock %}

{% block alpine_data %}storeInventory(){% endblock %}

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Inventory', subtitle='Manage your stock levels') %}
    <div class="flex items-center gap-4">
        {{ refresh_button(loading_var='loading', onclick='loadInventory()', variant='secondary') }}
    </div>
{% endcall %}

{{ loading_state('Loading inventory...') }}

{{ error_state('Error loading inventory') }}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Total Entries -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
            <span x-html="$icon('archive', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Total Entries</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.total_entries">0</p>
        </div>
    </div>

    <!-- Total Stock -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
            <span x-html="$icon('cube', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Total Stock</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatNumber(stats.total_quantity)">0</p>
        </div>
    </div>

    <!-- Low Stock -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-yellow-500 bg-yellow-100 rounded-full dark:text-yellow-100 dark:bg-yellow-500">
            <span x-html="$icon('exclamation-triangle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Low Stock</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.low_stock_count">0</p>
        </div>
    </div>

    <!-- Out of Stock -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-red-500 bg-red-100 rounded-full dark:text-red-100 dark:bg-red-500">
            <span x-html="$icon('x-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Out of Stock</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.out_of_stock_count">0</p>
        </div>
    </div>
</div>

<!-- Filters -->
<div x-show="!loading" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
    <div class="flex flex-wrap items-center gap-4">
        <!-- Search -->
        <div class="flex-1 min-w-[200px]">
            <div class="relative">
                <span class="absolute inset-y-0 left-0 flex items-center pl-3">
                    <span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
                </span>
                <input
                    type="text"
                    x-model="filters.search"
                    @input="debouncedSearch()"
                    placeholder="Search by product name or SKU..."
                    class="w-full pl-10 pr-4 py-2 text-sm text-gray-700 placeholder-gray-400 bg-gray-50 border border-gray-200 rounded-lg dark:placeholder-gray-500 dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                />
            </div>
        </div>

        <!-- Location Filter -->
        <select
            x-model="filters.location"
            @change="applyFilter()"
            class="px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        >
            <option value="">All Locations</option>
            <template x-for="loc in locations" :key="loc">
                <option :value="loc" x-text="loc"></option>
            </template>
        </select>

        <!-- Low Stock Filter -->
        <select
            x-model="filters.low_stock"
            @change="applyFilter()"
            class="px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
        >
            <option value="">All Stock Levels</option>
            <option value="true">Low Stock Only</option>
        </select>

        <!-- Clear Filters -->
        <button
            x-show="filters.search || filters.location || filters.low_stock"
            @click="clearFilters()"
            class="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200"
        >
            Clear filters
        </button>
    </div>
</div>

<!-- Bulk Actions Bar -->
<div x-show="!loading && selectedItems.length > 0"
     x-transition:enter="transition ease-out duration-200"
     x-transition:enter-start="opacity-0 -translate-y-2"
     x-transition:enter-end="opacity-100 translate-y-0"
     class="mb-4 p-3 bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-800 rounded-lg flex items-center justify-between">
    <div class="flex items-center gap-2">
        <span class="text-sm font-medium text-purple-700 dark:text-purple-300">
            <span x-text="selectedItems.length"></span> item(s) selected
        </span>
        <button @click="clearSelection()" class="text-sm text-purple-600 hover:text-purple-800 dark:text-purple-400">
            Clear
        </button>
    </div>
    <div class="flex items-center gap-2">
        <button
            @click="openBulkAdjustModal()"
            :disabled="saving"
            class="px-3 py-1.5 text-sm font-medium text-blue-700 bg-blue-100 rounded-lg hover:bg-blue-200 dark:bg-blue-900 dark:text-blue-300 dark:hover:bg-blue-800 disabled:opacity-50"
        >
            <span x-html="$icon('plus-minus', 'w-4 h-4 inline mr-1')"></span>
            Bulk Adjust
        </button>
        <button
            @click="exportSelectedItems()"
            class="px-3 py-1.5 text-sm font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600"
        >
            <span x-html="$icon('arrow-down-tray', 'w-4 h-4 inline mr-1')"></span>
            Export CSV
        </button>
    </div>
</div>

<!-- Inventory Table -->
<div x-show="!loading && !error" class="mb-8">
{% call table_wrapper() %}
    <thead>
        <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
            <th class="px-4 py-3 w-10">
                <input
                    type="checkbox"
                    :checked="allSelected"
                    :indeterminate="someSelected"
                    @click="toggleSelectAll()"
                    class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600"
                />
            </th>
            <th class="px-4 py-3">Product</th>
            <th class="px-4 py-3">SKU</th>
            <th class="px-4 py-3">Location</th>
            <th class="px-4 py-3">Quantity</th>
            <th class="px-4 py-3">Status</th>
            <th class="px-4 py-3">Actions</th>
        </tr>
    </thead>
    <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
        <template x-for="item in inventory" :key="item.id">
            <tr class="text-gray-700 dark:text-gray-400" :class="{'bg-purple-50 dark:bg-purple-900/10': isSelected(item.id)}">
                <!-- Checkbox -->
                <td class="px-4 py-3">
                    <input
                        type="checkbox"
                        :checked="isSelected(item.id)"
                        @click="toggleSelect(item.id)"
                        class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600"
                    />
                </td>
                <!-- Product -->
                <td class="px-4 py-3">
                    <div class="text-sm">
                        <p class="font-semibold" x-text="item.product_name || 'Unknown Product'"></p>
                    </div>
                </td>
                <!-- SKU -->
                <td class="px-4 py-3 text-sm font-mono" x-text="item.product_sku || '-'"></td>
                <!-- Location -->
                <td class="px-4 py-3 text-sm" x-text="item.location || 'Default'"></td>
                <!-- Quantity -->
                <td class="px-4 py-3 text-sm font-semibold" x-text="formatNumber(item.quantity)"></td>
                <!-- Status -->
                <td class="px-4 py-3 text-xs">
                    <span
                        :class="{
                            'px-2 py-1 font-semibold leading-tight rounded-full': true,
                            'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': getStockStatus(item) === 'ok',
                            'text-yellow-700 bg-yellow-100 dark:bg-yellow-700 dark:text-yellow-100': getStockStatus(item) === 'low',
                            'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100': getStockStatus(item) === 'out'
                        }"
                    >
                        <span x-text="getStockStatus(item) === 'out' ? 'Out of Stock' : (getStockStatus(item) === 'low' ? 'Low Stock' : 'In Stock')"></span>
                    </span>
                </td>
                <!-- Actions -->
                <td class="px-4 py-3">
                    <div class="flex items-center space-x-2 text-sm">
                        <button
                            @click="openAdjustModal(item)"
                            class="p-1 text-gray-500 hover:text-blue-600 dark:text-gray-400 dark:hover:text-blue-400"
                            title="Adjust Stock"
                        >
                            <span x-html="$icon('plus-minus', 'w-5 h-5')"></span>
                        </button>
                        <button
                            @click="openSetModal(item)"
                            class="p-1 text-gray-500 hover:text-purple-600 dark:text-gray-400 dark:hover:text-purple-400"
                            title="Set Quantity"
                        >
                            <span x-html="$icon('pencil', 'w-5 h-5')"></span>
                        </button>
                    </div>
                </td>
            </tr>
        </template>
        <!-- Empty State -->
        <tr x-show="inventory.length === 0">
            <td colspan="7" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                <div class="flex flex-col items-center">
                    <span x-html="$icon('archive', 'w-12 h-12 text-gray-300 dark:text-gray-600 mb-4')"></span>
                    <p class="text-lg font-medium">No inventory found</p>
                    <p class="text-sm">Add products and set their stock levels</p>
                </div>
            </td>
        </tr>
    </tbody>
{% endcall %}
</div>

<!-- Pagination -->
{{ pagination(show_condition="!loading && pagination.total > 0") }}

<!-- Adjust Stock Modal -->
{% call modal_simple('adjustStockModal', 'Adjust Stock', show_var='showAdjustModal', size='sm') %}
<div class="space-y-4">
    <template x-if="selectedItem">
        <div>
            <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
                Adjust stock for <span class="font-semibold" x-text="selectedItem.product_name"></span>
                <span class="text-xs text-gray-500">(Current: <span x-text="selectedItem.quantity"></span>)</span>
            </p>
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Adjustment (+ or -)</label>
                {# noqa: FE-008 - Adjustment input accepts +/- values, not a quantity stepper #}
                <input
                    type="number"
                    x-model.number="adjustForm.quantity"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    placeholder="e.g., 10 or -5"
                />
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Reason (optional)</label>
                <input
                    type="text"
                    x-model="adjustForm.reason"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    placeholder="e.g., Restock, Damaged goods"
                />
            </div>
        </div>
    </template>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showAdjustModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="executeAdjust()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
        >Adjust</button>
    </div>
</div>
{% endcall %}

<!-- Set Quantity Modal -->
{% call modal_simple('setQuantityModal', 'Set Quantity', show_var='showSetModal', size='sm') %}
<div class="space-y-4">
    <template x-if="selectedItem">
        <div>
            <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
                Set quantity for <span class="font-semibold" x-text="selectedItem.product_name"></span>
            </p>
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">New Quantity</label>
                <input
                    type="number"
                    x-model.number="setForm.quantity"
                    min="0"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                />
            </div>
        </div>
    </template>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showSetModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="executeSet()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
        >Set</button>
    </div>
</div>
{% endcall %}

<!-- Bulk Adjust Modal -->
{% call modal_simple('bulkAdjustModal', 'Bulk Adjust Stock', show_var='showBulkAdjustModal', size='sm') %}
<div class="space-y-4">
    <div>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
            Adjust stock for <span class="font-semibold" x-text="selectedItems.length"></span> selected item(s)
        </p>
        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Adjustment (+ or -)</label>
            <input
                type="number"
                x-model.number="bulkAdjustForm.quantity"
                class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                placeholder="e.g., 10 or -5"
            />
        </div>
        <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Reason (optional)</label>
            <input
                type="text"
                x-model="bulkAdjustForm.reason"
                class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                placeholder="e.g., Restock, Damaged goods"
            />
        </div>
    </div>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showBulkAdjustModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="bulkAdjust()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
        >Adjust All</button>
    </div>
</div>
{% endcall %}
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('inventory_static', path='store/js/inventory.js') }}"></script>
{% endblock %}
