{% extends 'base.html' %}
{% block title %}{{ product.name }}{% endblock %}

{% block content %}
<div class="flex justify-between items-center mb-6">
  <h1 class="text-2xl font-bold">{{ product.name }}</h1>
  <div class="flex gap-2">
    <a href="{% url 'inventory:stock_adjust' product.pk %}" class="btn btn-outline btn-sm">📦 Adjust Stock</a>
    <a href="{% url 'inventory:product_edit' product.pk %}" class="btn btn-primary btn-sm">✏ Edit</a>
  </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-4 mb-6">

  <div class="stat bg-base-100 shadow rounded-box">
    <div class="stat-title">Current Stock</div>
    <div class="stat-value {% if product.is_low_stock %}text-warning{% else %}text-success{% endif %}">
      {{ product.stock_quantity }}
    </div>
    <div class="stat-desc">{{ product.get_unit_display }}</div>
  </div>

  <div class="stat bg-base-100 shadow rounded-box">
    <div class="stat-title">Cost Price</div>
    <div class="stat-value text-2xl">{{ business.currency }} {{ product.cost_price|floatformat:0 }}</div>
    <div class="stat-desc">Per {{ product.get_unit_display }}</div>
  </div>

  <div class="stat bg-base-100 shadow rounded-box">
    <div class="stat-title">Selling Price</div>
    <div class="stat-value text-2xl text-primary">{{ business.currency }} {{ product.selling_price|floatformat:0 }}</div>
    <div class="stat-desc">Stock value: {{ business.currency }} {{ product.stock_value|floatformat:0 }}</div>
  </div>

</div>

<!-- Stock Movements -->
<div class="card bg-base-100 shadow">
  <div class="card-body">
    <h2 class="card-title text-base">Stock History</h2>
    {% if scoped_branch %}
      <p class="text-sm opacity-70 mb-2">Showing movements for branch: {{ scoped_branch.name }}</p>
    {% endif %}
    <table class="table table-sm">
      <thead>
        <tr><th>Type</th><th>Qty</th><th>Reference</th><th>Note</th><th>Date</th><th>By</th></tr>
      </thead>
      <tbody>
        {% for move in movements %}
        <tr>
          <td>
            <span class="badge badge-sm
              {% if move.movement_type == 'stock_in' %}badge-success
              {% elif move.movement_type == 'sale' %}badge-primary
              {% else %}badge-ghost{% endif %}">
              {{ move.get_movement_type_display }}
            </span>
          </td>
          <td>{{ move.quantity }}</td>
          <td class="font-mono text-xs">{{ move.reference|default:"—" }}</td>
          <td class="text-base-content/60 text-xs">{{ move.note|default:"—" }}</td>
          <td>{{ move.date|date:"d M Y" }}</td>
          <td>{{ move.created_by.username|default:"—" }}</td>
        </tr>
        {% empty %}
        <tr><td colspan="6" class="text-center text-base-content/40">No movements yet</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endblock %}
