{% extends "core/base.html" %}
{% load static %}
{% block title %}Stock Forecast - SwiftPOS{% endblock %}
{% block topbar_title %}Stock Forecast{% endblock %}

{% block content %}
<div class="container-fluid py-4">

    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <div>
            <h5 class="mb-0 fw-bold">📦 Smart Stock Forecast</h5>
            <p class="text-muted small mb-0">Predicted stock exhaustion based on the last 30 days of sales velocity.</p>
        </div>
        <a href="{% url 'sales:pos' %}" class="btn btn-sm btn-outline-secondary">
            <i class="fas fa-cash-register me-1"></i> Back to POS
        </a>
    </div>

    <!-- Summary Cards -->
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center p-3">
                <div class="text-danger fs-3 fw-bold">{{ counts.critical|default:0 }}</div>
                <div class="small text-muted">Critical</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center p-3">
                <div class="text-warning fs-3 fw-bold">{{ counts.low_risk|default:0 }}</div>
                <div class="small text-muted">Low Risk</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center p-3">
                <div class="text-secondary fs-3 fw-bold">{{ counts.dead|default:0 }}</div>
                <div class="small text-muted">Dead Stock</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center p-3">
                <div class="text-success fs-3 fw-bold">{{ counts.healthy|default:0 }}</div>
                <div class="small text-muted">Healthy</div>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="mb-3 d-flex gap-2 flex-wrap">
        <a href="{% url 'analytics:stock_forecast' %}" class="btn btn-sm {% if not health_filter %}btn-primary{% else %}btn-outline-secondary{% endif %}">All</a>
        {% for val, label in health_choices %}
        <a href="?health={{ val }}" class="btn btn-sm {% if health_filter == val %}btn-primary{% else %}btn-outline-secondary{% endif %}">{{ label }}</a>
        {% endfor %}
    </div>

    <!-- Forecast Table -->
    {% if forecasts %}
    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Product</th>
                            <th class="text-center">Current Stock</th>
                            <th class="text-center">Avg Daily Sales</th>
                            <th class="text-center">Days Until Stockout</th>
                            <th class="text-center">Restock Qty</th>
                            <th class="text-center">Health</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for fc in forecasts %}
                        <tr>
                            <td>
                                <div class="fw-semibold">{{ fc.product.name }}</div>
                                <small class="text-muted">{{ fc.product.sku }}</small>
                            </td>
                            <td class="text-center fw-bold">{{ fc.product.stock_quantity }}</td>
                            <td class="text-center">{{ fc.avg_daily_sales|floatformat:1 }}</td>
                            <td class="text-center">
                                {% if fc.days_until_stockout is not None %}
                                    <span class="fw-bold {% if fc.days_until_stockout <= 3 %}text-danger{% elif fc.days_until_stockout <= 14 %}text-warning{% else %}text-success{% endif %}">
                                        {{ fc.days_until_stockout }} day{{ fc.days_until_stockout|pluralize }}
                                    </span>
                                {% else %}
                                    <span class="text-muted">No recent sales</span>
                                {% endif %}
                            </td>
                            <td class="text-center">
                                {% if fc.recommended_restock_qty > 0 %}
                                    <span class="badge bg-primary-subtle text-primary border border-primary-subtle">
                                        {{ fc.recommended_restock_qty }} units
                                    </span>
                                {% else %}
                                    <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td class="text-center">
                                {% if fc.stock_health == 'critical' %}
                                    <span class="badge bg-danger">🔴 Critical</span>
                                {% elif fc.stock_health == 'low_risk' %}
                                    <span class="badge bg-warning text-dark">🟡 Low Risk</span>
                                {% elif fc.stock_health == 'dead' %}
                                    <span class="badge bg-secondary">💀 Dead Stock</span>
                                {% elif fc.stock_health == 'overstock' %}
                                    <span class="badge bg-info text-dark">📦 Overstock</span>
                                {% else %}
                                    <span class="badge bg-success">✅ Healthy</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% else %}
    <div class="card border-0 shadow-sm">
        <div class="card-body text-center py-5">
            <i class="fas fa-box-open fa-3x text-muted opacity-25 d-block mb-3"></i>
            <p class="text-muted">No forecast data yet. Run <code>python manage.py generate_insights</code> to generate forecasts.</p>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
