{# fortunaisk/templates/fortunaisk/lottery_detail.html #}
{% extends "fortunaisk/base.html" %}
{% load i18n humanize fortunaisk_tags static %}

{% block page_title %}
    {% trans "Lottery Detail" %}
{% endblock page_title %}

{% block details %}
<div class="container my-5">
    <!-- Page Header -->
    <div class="mb-5 text-center">
        <h2 class="fw-bold" style="color: #f1c40f;">
            <i class="fas fa-trophy me-2" style="color: #27ae60;"></i>
            {% trans "Lottery Detail" %}: <strong>{{ lottery.lottery_reference }}</strong>
        </h2>
        <p class="text-muted fs-5">
            {% trans "Review the details and manage the lottery settings." %}
        </p>
    </div>

    <!-- Action Buttons -->
    <div class="mb-4 d-flex justify-content-end">
        {% if perms.fortunaisk.can_admin_app and lottery.status == 'active' %}
        <form action="{% url 'fortunaisk:terminate_lottery' lottery.id %}" method="post" class="me-2">
            {% csrf_token %}
            <button type="submit" class="btn btn-outline-danger btn-sm"
                    onclick="return confirm('{% trans "Are you sure you want to cancel this lottery prematurely?" %}');">
                <i class="fas fa-stop-circle"></i> {% trans "Cancel" %}
            </button>
        </form>
        {% endif %}
        <a href="{% url 'fortunaisk:lottery_participants' lottery.id %}" class="btn btn-outline-primary btn-sm">
            <i class="fas fa-users"></i> {% trans "View Participants" %}
        </a>
    </div>

    <!-- Lottery Information Section -->
    <div class="row mb-5">
        <!-- General Information Card -->
        <div class="col-lg-6 mb-4">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-primary text-white">
                    <i class="fas fa-info-circle"></i> {% trans "Lottery Information" %}
                </div>
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Status" %}</strong>
                        {% if lottery.status == "active" %}
                            <span class="badge bg-success">{% trans "Active" %}</span>
                        {% elif lottery.status == "completed" %}
                            <span class="badge bg-secondary">{% trans "Completed" %}</span>
                        {% elif lottery.status == "pending" %}
                            <span class="badge bg-info"><i class="fas fa-spinner fa-spin me-1"></i>{% trans "Pending" %}</span>
                        {% elif lottery.status == "cancelled" %}
                            <span class="badge bg-danger">{% trans "Cancelled" %}</span>
                        {% endif %}
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Ticket Price (ISK)" %}</strong>
                        <span>{{ lottery.ticket_price|intcomma }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Tax (%)" %}</strong>
                        <span>{{ lottery.tax|floatformat:2 }} %</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Total Pot (ISK)" %}</strong>
                        <span>{{ lottery.total_pot|intcomma }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Tax Collected (ISK)" %}</strong>
                        <span>{{ tax_collected|intcomma }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Winner Count" %}</strong>
                        <span>{{ lottery.winner_count }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Max Tickets per User" %}</strong>
                        <span>
                            {% if lottery.max_tickets_per_user %}
                                {{ lottery.max_tickets_per_user }}
                            {% else %}
                                <span class="text-muted">{% trans "Unlimited" %}</span>
                            {% endif %}
                        </span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Start Date" %}</strong>
                        <span>{{ lottery.start_date|date:"Y-m-d H:i" }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "End Date" %}</strong>
                        <span>{{ lottery.end_date|date:"Y-m-d H:i" }}</span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Additional Information Card -->
        <div class="col-lg-6 mb-4">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-success text-white">
                    <i class="fas fa-cogs"></i> {% trans "Additional Information" %}
                </div>
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Payment Receiver" %}</strong>
                        <span>
                            {% if lottery.payment_receiver %}
                                {{ lottery.payment_receiver }}
                            {% else %}
                                <span class="text-muted">{% trans "Unknown" %}</span>
                            {% endif %}
                        </span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Duration" %}</strong>
                        <span>
                            {{ lottery.duration_value }} {{ lottery.get_duration_unit_display }}
                        </span>
                    </li>
                    <li class="list-group-item">
                        <strong>{% trans "Winners Distribution" %}:</strong>
                        <div class="mt-2">
                            <ul class="list-unstyled">
                                {% for dist in distributions %}
                                <li class="d-flex align-items-center mb-2">
                                    <i class="fas fa-award me-2 text-warning"></i>
                                    <span class="fw-bold">
                                        {% trans "Winner" %} {{ dist.winner_rank }}:
                                    </span>
                                    <span class="ms-2 badge bg-info text-light">
                                        {{ dist.winner_prize_distribution }}%
                                    </span>
                                </li>
                                {% empty %}
                                <li class="text-muted">{% trans "No distribution defined." %}</li>
                                {% endfor %}
                            </ul>
                        </div>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Participants" %}</strong>
                        <span>{{ participant_count }}</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <strong>{% trans "Tickets Sold" %}</strong>
                        <span>{{ tickets_sold }}</span>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <!-- Winners Table Section -->
    <div class="row mb-5">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-warning text-white d-flex justify-content-between align-items-center">
                    <h4 class="mb-0">
                        <i class="fas fa-award"></i> {% trans "Winners" %}
                    </h4>
                </div>
                <div class="card-body">
                    {% if winners %}
                        <div class="table-responsive">
                            <table class="table table-striped table-hover align-middle">
                                <thead class="table-dark">
                                    <tr>
                                        <th>{% trans "Reference" %}</th>
                                        <th>{% trans "User" %}</th>
                                        <th>{% trans "Character" %}</th>
                                        <th>{% trans "Prize (ISK)" %}</th>
                                        <th>{% trans "Won At" %}</th>
                                        <th>{% trans "Distributed" %}</th>
                                        <th>{% trans "Actions" %}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for winner in winners %}
                                    <tr>
                                        <td>{{ winner.ticket.lottery.lottery_reference }}</td>
                                        <td>{{ winner.ticket.user.username }}</td>
                                        <td>
                                            {% if winner.character %}
                                                {{ winner.character }}
                                            {% else %}
                                                <span class="text-muted">{% trans "N/A" %}</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ winner.prize_amount|floatformat:2|intcomma }}</td>
                                        <td>{{ winner.won_at|date:"Y-m-d H:i" }}</td>
                                        <td>
                                            {% if winner.distributed %}
                                                <span class="badge bg-success">
                                                    <i class="fas fa-check-circle me-1"></i> {% trans "Yes" %}
                                                </span>
                                            {% else %}
                                                <span class="badge bg-warning text-dark">
                                                    <i class="fas fa-exclamation-triangle me-1"></i> {% trans "No" %}
                                                </span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if perms.fortunaisk.can_admin_app and not winner.distributed %}
                                            <form action="{% url 'fortunaisk:distribute_prize' winner.id %}"
                                                  method="post" class="d-inline">
                                                {% csrf_token %}
                                                <button type="submit" class="btn btn-sm btn-primary"
                                                        onclick="return confirm('{% trans "Confirm distribution?" %}');">
                                                    <i class="fas fa-hand-holding-usd"></i>
                                                </button>
                                            </form>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="alert alert-info text-center">
                            <i class="fas fa-info-circle me-2"></i>
                            {% trans "No winners recorded yet." %}
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock details %}
