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

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

{% block details %}
<div class="container mt-4">
    <h2 class="text-center mb-4">
        {% trans "Lottery Detail" %}: {{ lottery.lottery_reference }}
    </h2>

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

    <!-- Lottery Info -->
    <div class="row mb-4">
        <div class="col-md-6">
            <ul class="list-group">
                <!-- Status -->
                <li class="list-group-item">
                    <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 == "cancelled" %}
                        <span class="badge bg-danger">{% trans "Cancelled" %}</span>
                    {% else %}
                        {{ lottery.status }}
                    {% endif %}
                </li>
                <!-- Ticket Price -->
                <li class="list-group-item">
                    <strong>{% trans "Ticket Price (ISK)" %}:</strong>
                    {{ lottery.ticket_price|floatformat:2 }}
                </li>
                <!-- Total Pot -->
                <li class="list-group-item">
                    <strong>{% trans "Total Pot (ISK)" %}:</strong>
                    {{ lottery.total_pot|intcomma }}
                </li>
                <!-- Winner Count -->
                <li class="list-group-item">
                    <strong>{% trans "Winner Count" %}:</strong>
                    {{ lottery.winner_count }}
                </li>
                <!-- Max Tickets per User -->
                <li class="list-group-item">
                    <strong>{% trans "Max Tickets per User" %}:</strong>
                    {% if lottery.max_tickets_per_user %}
                        {{ lottery.max_tickets_per_user }}
                    {% else %}
                        {% trans "Unlimited" %}
                    {% endif %}
                </li>
                <!-- Start / End Date -->
                <li class="list-group-item">
                    <strong>{% trans "Start Date" %}:</strong>
                    {{ lottery.start_date|date:"Y-m-d H:i" }}
                </li>
                <li class="list-group-item">
                    <strong>{% trans "End Date" %}:</strong>
                    {{ lottery.end_date|date:"Y-m-d H:i" }}
                </li>
            </ul>
        </div>

        <div class="col-md-6">
            <ul class="list-group">
                <!-- Payment Receiver -->
                <li class="list-group-item">
                    <strong>{% trans "Payment Receiver" %}:</strong>
                    {% if lottery.payment_receiver %}
                        {{ lottery.payment_receiver }}
                    {% else %}
                        {% trans "Unknown" %}
                    {% endif %}
                </li>
                <!-- Duration -->
                <li class="list-group-item">
                    <strong>{% trans "Duration" %}:</strong>
                    {{ lottery.duration_value }} {{ lottery.get_duration_unit_display }}
                </li>
                <!-- Winners Distribution -->
                <li class="list-group-item">
                    <strong>{% trans "Winners Distribution" %}:</strong>
                    {% for percentage in lottery.winners_distribution %}
                        {{ percentage }}{% if not forloop.last %}, {% endif %}
                    {% endfor %}
                </li>
                <!-- Number of Participants -->
                <li class="list-group-item">
                    <strong>{% trans "Number of Participants" %}:</strong>
                    {{ lottery.ticket_purchases.count }}
                </li>
            </ul>
        </div>
    </div>

    <!-- Winners Table -->
    <div class="row mb-4">
        <div class="col-md-12">
            <h4>{% trans "Winners" %}</h4>
            {% if winners %}
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="table-success">
                            <tr>
                                <th scope="col">{% trans "Lottery Reference" %}</th>
                                <th scope="col">{% trans "User" %}</th>
                                <th scope="col">{% trans "Character" %}</th>
                                <th scope="col">{% trans "Prize Amount (ISK)" %}</th>
                                <th scope="col">{% trans "Won At" %}</th>
                                <th scope="col">{% trans "Distributed" %}</th>
                                <th scope="col">{% 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 %}
                                        N/A
                                    {% endif %}
                                </td>
                                <td>
                                    {{ winner.prize_amount|floatformat:2 }}
                                </td>
                                <td>
                                    {{ winner.won_at|date:"Y-m-d H:i" }}
                                </td>
                                <td>
                                    {% if winner.distributed %}
                                        <span class="badge bg-success">
                                            {% trans "Yes" %}
                                        </span>
                                    {% else %}
                                        <span class="badge bg-warning">
                                            {% trans "No" %}
                                        </span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if not winner.distributed %}
                                        <form action="{% url 'fortunaisk:distribute_prize' winner.id %}"
                                                method="post"
                                                style="display:inline;">
                                            {% csrf_token %}
                                            <button type="submit"
                                                    class="btn btn-sm btn-primary"
                                                    onclick="return confirm('{% trans "Have you distributed the prize to this winner?" %}');">
                                                <i class="fas fa-hand-holding-usd"></i>
                                                {% trans "Distribute Prize" %}
                                            </button>
                                        </form>
                                    {% else %}
                                        <span class="badge bg-success">
                                            {% trans "Distributed" %}
                                        </span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <div class="alert alert-info text-center">
                    <i class="fas fa-info-circle alert-icon"></i>
                    {% trans "No winners recorded yet." %}
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock details %}
