{% extends 'core/base.html' %}
{% load static %}

{% block title %}Inventory Dashboard - SwiftPOS{% endblock %}

{% block topbar_title %}
Inventory
{% endblock %}

{% block content %}
<div class="container-fluid px-0">
    <!-- Toast Messages -->
    <div class="swiftpos-toast-container">
        {% if messages %}
            {% for message in messages %}
                <div class="swiftpos-toast swiftpos-toast-{{ message.tags }}">
                    <div class="toast-icon">
                        {% if message.tags == 'success' %}
                            <i class="fas fa-check-circle"></i>
                        {% elif message.tags == 'error' or message.tags == 'danger' %}
                            <i class="fas fa-exclamation-circle"></i>
                        {% elif message.tags == 'warning' %}
                            <i class="fas fa-exclamation-triangle"></i>
                        {% else %}
                            <i class="fas fa-info-circle"></i>
                        {% endif %}
                    </div>
                    <div class="toast-text">
                        {{ message }}
                    </div>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    <!-- Metric cards -->
    <div class="row g-3 mb-3">
        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted">Total Products</div>
                        <div class="h4 mb-1">{{ total_products }}</div>
                        <div class="small text-muted">Unique SKUs</div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(79,70,229,.08);color:var(--sp-primary);">
                        <i class="fas fa-box"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted">Total Stock Units</div>
                        <div class="h4 mb-1">{{ total_stock }}</div>
                        <div class="small text-muted">All products combined</div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(34,197,94,.1);color:var(--sp-secondary);">
                        <i class="fas fa-cubes"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted">Low Stock</div>
                        <div class="h4 mb-1 text-warning">{{ low_stock_count }}</div>
                        <div class="small text-muted">Needs attention</div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(250,204,21,.16);color:var(--sp-warning);">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-lg-3">
            <div class="card h-100">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="small text-muted">Out of Stock</div>
                        <div class="h4 mb-1 text-danger">{{ out_of_stock_count }}</div>
                        <div class="small text-muted">Unavailable items</div>
                    </div>
                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                         style="width:44px;height:44px;background:rgba(220,38,38,.14);color:var(--sp-error);">
                        <i class="fas fa-times-circle"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Low stock & out-of-stock lists -->
    <div class="row g-3">
        <div class="col-lg-6">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                        <i class="fas fa-arrow-down-wide-short"></i> Low Stock Alert
                    </h6>
                    <a href="{% url 'inventory:product_list' %}" class="small text-muted text-decoration-none">
                        View all
                    </a>
                </div>
                <div class="card-body">
                    {% if low_stock_products %}
                        {% for product in low_stock_products %}
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <div>
                                    <div class="fw-semibold small">{{ product.name }}</div>
                                    <div class="text-muted small">{{ product.sku }}</div>
                                </div>
                                <div class="text-end">
                                    <span class="badge bg-warning text-dark">
                                        {{ product.stock_quantity }}
                                    </span>
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted small mb-0 text-center">
                            No low stock products 🎉
                        </p>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                        <i class="fas fa-ban"></i> Out of Stock
                    </h6>
                    <a href="{% url 'inventory:product_list' %}" class="small text-muted text-decoration-none">
                        Restock
                    </a>
                </div>
                <div class="card-body">
                    {% if out_of_stock_products %}
                        {% for product in out_of_stock_products %}
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <div>
                                    <div class="fw-semibold small">{{ product.name }}</div>
                                    <div class="text-muted small">{{ product.sku }}</div>
                                </div>
                                <div class="text-end">
                                    <span class="badge bg-danger">
                                        {{ product.stock_quantity }}
                                    </span>
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <p class="text-muted small mb-0 text-center">
                            No out of stock products.
                        </p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}