{% extends "base.html" %}
{% load static i18n %}

{% block title %}{% trans "Interviews & Meetings" %} - {{ block.super }}{% endblock %}

{% block customCSS %}
<style>
    /* UI Variables for the KAAT-S Theme (Consistent with Reference) */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-gray-light: #f8f9fa;
        --kaauh-warning: #ffc107; 
        --kaauh-danger: #dc3545;
        --kaauh-success: #28a745;
    }

    /* Enhanced Card Styling */
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        transition: transform 0.2s, box-shadow 0.2s;
        background-color: white;
    }
    .card:not(.no-hover):hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.1);
    }
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.25rem;
    }
    .btn-main-action:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }
    .btn-outline-secondary {
        color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-secondary:hover {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-teal-dark);
    }
    .btn-outline-primary {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
    }
    .btn-outline-primary:hover {
        background-color: var(--kaauh-teal);
        color: white;
    }
    .meeting-card .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 600;
        font-size: 1.15rem;
    }
    .meeting-card .card-text i {
        color: var(--kaauh-teal);
        width: 1.25rem;
    }
    .text-primary-theme { color: var(--kaauh-teal) !important; }
    .status-badge {
        font-size: 0.8rem;
        padding: 0.4em 0.8em;
        border-radius: 0.4rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.7px;
    }
    /* Statuses */
    .bg-waiting { background-color: var(--kaauh-warning) !important; color: var(--kaauh-primary-text) !important;}
    .bg-started { background-color: var(--kaauh-teal) !important; color: white !important;}
    .bg-ended { background-color: var(--kaauh-danger) !important; color: white !important;}
    .bg-scheduled { background-color: #5bc0de !important; color: white !important;}
    /* Event Types */
    .bg-Remote { background-color: var(--kaauh-teal) !important; color: white !important; }
    .bg-Onsite { background-color: #007bff !important; color: white !important; }

    /* Table Styling */
    .table-view .table thead th {
        background-color: var(--kaauh-teal-dark);
        color: white;
        font-weight: 600;
        border-color: var(--kaauh-border);
        text-transform: uppercase;
        font-size: 0.8rem;
        letter-spacing: 0.5px;
        padding: 1rem;
    }
    .table-view .table tbody td {
        vertical-align: middle;
        padding: 1rem;
        border-color: var(--kaauh-border);
    }
    .table-view .table tbody tr:hover {
        background-color: var(--kaauh-gray-light);
    }
    .pagination .page-item.active .page-link {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 style="color: var(--kaauh-teal-dark); font-weight: 700;">
            <i class="fas fa-calendar-alt me-2"></i> {% trans "Interviews & Meetings" %}
        </h1>
        {% comment %} <div class="btn-group" role="group">
            <a href="{% url 'create_remote_meeting' %}" class="btn btn-main-action">
                <i class="fas fa-globe me-1"></i> {% trans "Create Remote" %}
            </a>
            <a href="{% url 'create_onsite_meeting' %}" class="btn btn-outline-secondary">
                <i class="fas fa-building me-1"></i> {% trans "Create Onsite" %}
            </a>
        </div> {% endcomment %}
    </div>

    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <label for="search" class="form-label small text-muted">{% trans "Search by Topic" %}</label>
                    <div class="input-group input-group-lg mb-3">
                        <form method="get" action="" class="w-100">
                            {# Assuming includes/search_form.html handles the 'q' parameter #}
                            {% include "includes/search_form.html" with search_query=search_query %}
                        </form>
                    </div>
                </div>
                <div class="col-md-8">
                    <form method="GET" class="row g-3 align-items-end" >
                        {# Hidden inputs to persist other filters #}
                        {% if search_query %}<input type="hidden" name="q" value="{{ search_query }}">{% endif %}
                        {% if status_filter %}<input type="hidden" name="status" value="{{ status_filter }}">{% endif %}
                        {% if candidate_name_filter %}<input type="hidden" name="candidate_name" value="{{ candidate_name_filter }}">{% endif %}

                        <div class="col-md-3">
                            <label for="type_filter" class="form-label small text-muted">{% trans "Interview Type" %}</label>
                            <select name="type" id="type_filter" class="form-select form-select-sm">
                                <option value="">{% trans "All Types" %}</option>
                                <option value="Remote" {% if type_filter == 'Remote' %}selected{% endif %}>{% trans "Remote" %}</option>
                                <option value="Onsite" {% if type_filter == 'Onsite' %}selected{% endif %}>{% trans "Onsite" %}</option>
                            </select>
                        </div>
                        
                        <div class="col-md-3">
                            <label for="status" class="form-label small text-muted">{% trans "Filter by Status" %}</label>
                            <select name="status" id="status" class="form-select form-select-sm">
                                <option value="">{% trans "All Statuses" %}</option>
                                {# CORRECTED: Using the context variable passed from the view #}
                                {% for choice, display in status_choices %}
                                    <option value="{{ choice }}" {% if status_filter == choice %}selected{% endif %}>{{ display }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="candidate_name" class="form-label small text-muted">{% trans "Candidate Name" %}</label>
                            <input type="text" class="form-control form-control-sm" id="candidate_name" name="candidate_name" placeholder="{% trans 'Search by candidate...' %}" value="{{ candidate_name_filter }}">
                        </div>
                        <div class="col-md-3">
                            <div class="filter-buttons">
                                <button type="submit" class="btn btn-main-action btn-sm">
                                    <i class="fas fa-filter me-1"></i> {% trans "Apply Filters" %}
                                </button>
                                {% if status_filter or search_query or candidate_name_filter or type_filter %}
                                    <a href="{% url 'list_meetings' %}" class="btn btn-outline-secondary btn-sm">
                                        <i class="fas fa-times me-1"></i> {% trans "Clear" %}
                                    </a>
                                {% endif %}
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    {% if meetings_data %}
        <div id="meetings-list">
            {# View Switcher (not provided, assuming standard include) #}
            {% include "includes/_list_view_switcher.html" with list_id="meetings-list" %}

            {# Card View #}
            <div class="card-view active row">
                {% for meeting in meetings_data %}
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card meeting-card h-100 shadow-sm">
                            <div class="card-body d-flex flex-column">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <h5 class="card-title flex-grow-1 me-3"><a href="" class="text-decoration-none text-primary-theme">{{ meeting.topic }}</a></h5>
                                    {# Display the type badge (Remote/Onsite) #}
                                    <span class="status-badge bg-{{ meeting.type }}">
                                        {{ meeting.type|title }}
                                    </span>
                                </div>

                                <p class="card-text text-muted small mb-3">
                                    <i class="fas fa-user"></i> {% trans "Candidate" %}: {{ meeting.interview.application.person.full_name|default:"N/A" }}<br>
                                    <i class="fas fa-briefcase"></i> {% trans "Job" %}: {{ meeting.interview.job.title|default:"N/A" }}<br>
                                    
                                    {# Dynamic location/type details #}
                                    {% if meeting.type == 'Remote' %}
                                        <i class="fas fa-link"></i> {% trans "Remote ID" %}: {{ meeting.meeting_id|default:meeting.location.id }}<br>
                                    {% elif meeting.type == 'Onsite' %}
                                        {# Use the details object for concrete location info #}
                                        <i class="fas fa-map-marker-alt"></i> {% trans "Location" %}: {{ meeting.details.room_number|default:meeting.details.physical_address|truncatechars:30 }}<br>
                                    {% endif %}
                                    <i class="fas fa-clock"></i> {% trans "Start" %}: {{ meeting.start_time|date:"M d, Y H:i" }}<br>
                                    <i class="fas fa-stopwatch"></i> {% trans "Duration" %}: {{ meeting.duration }} minutes
                                </p>
                                
                                <span class="status-badge bg-{{ meeting.status }}">
                                    {{ meeting.interview.get_status_display }}
                                </span>

                                <div class="mt-auto pt-2 border-top">
                                   <div class="d-flex gap-2">
                                        <a href="" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i> {% trans "View" %}
                                        </a>

                                        {% if meeting.type == 'Remote' and meeting.join_url %}
                                            <a href="{{ meeting.join_url }}" target="_blank" class="btn btn-sm btn-main-action">
                                                <i class="fas fa-sign-in-alt"></i> {% trans "Join Remote" %}
                                            </a>
                                        {% elif meeting.type == 'Onsite' %}
                                             <button type="button" class="btn btn-sm btn-outline-secondary" disabled>
                                                <i class="fas fa-check"></i> {% trans "Physical Event" %}
                                            </button>
                                        {% endif %}
                                        
                                        {# CORRECTED: Passing the slug to the update URL #}
                                        <a href="" class="btn btn-sm btn-outline-secondary">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button type="button" class="btn btn-outline-danger btn-sm" title="{% trans 'Delete' %}"
                                                data-bs-toggle="modal" data-bs-target="#deleteModal"
                                                hx-post=""
                                                hx-target="#deleteModalBody"
                                                hx-swap="outerHTML"
                                                data-item-name="{{ meeting.topic }}">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>

            {# Table View #}
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col">{% trans "Topic" %}</th>
                                <th scope="col">{% trans "Type" %}</th>
                                <th scope="col">{% trans "Candidate" %}</th>
                                <th scope="col">{% trans "Job" %}</th>
                                <th scope="col">{% trans "Start Time" %}</th>
                                <th scope="col">{% trans "Duration" %}</th>
                                <th scope="col">{% trans "Status" %}</th>
                                <th scope="col" class="text-end">{% trans "Actions" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for meeting in meetings_data %}
                            <tr>
                                <td><strong class="text-primary"><a href="" class="text-decoration-none text-secondary">{{ meeting.topic }}<a></strong></td>
                                <td>
                                    {# Display the event type badge #}
                                    <span class="status-badge bg-{{ meeting.type }}">{{ meeting.type|title }}</span>
                                </td>
                                <td>
                                    <a class="text-primary text-decoration-none" href="{% url 'candidate_detail' meeting.interview.application.person.slug %}">{{ meeting.interview.application.person.full_name }} <i class="fas fa-link"></i></a>
                                </td>
                                <td>
                                    <a class="text-primary text-decoration-none" href="{% url 'job_detail' meeting.interview.job.slug %}">{{ meeting.interview.job.title }} <i class="fas fa-link"></i></a>
                                </td>
                                <td>{{ meeting.start_time|date:"M d, Y H:i" }}</td>
                                <td>{{ meeting.duration }} min</td>
                                <td>
                                    {# Display the meeting status badge from the ScheduledInterview model #}
                                    <span class="status-badge bg-{{ meeting.status }}">
                                        {{ meeting.interview.get_status_display }}
                                    </span>
                                </td>
                                <td class="text-end">
                                    <div class="btn-group btn-group-sm" role="group">
                                        {% if meeting.type == 'Remote' and meeting.join_url %}
                                            <a href="{{ meeting.join_url }}" target="_blank" class="btn btn-main-action" title="{% trans 'Join' %}">
                                                <i class="fas fa-sign-in-alt"></i>
                                            </a>
                                        {% endif %}
                                        <a href="{% url 'meeting_details' meetings.first.interview_location.slug%}" class="btn btn-outline-primary" title="{% trans 'View' %}">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        {# CORRECTED: Passing the slug to the update URL #}
                                        <a href="" class="btn btn-outline-secondary" title="{% trans 'Update' %}">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button type="button" class="btn btn-outline-danger" title="{% trans 'Delete' %}"
                                                data-bs-toggle="modal"
                                                data-bs-target="#deleteModal"
                                                hx-post=""
                                                hx-target="#deleteModalBody"
                                                hx-swap="outerHTML"
                                                data-item-name="{{ meeting.topic }}">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        {# Pagination (All filters correctly included in query strings) #}
        {% if is_paginated %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}{% if type_filter %}&type={{ type_filter }}{% endif %}{% if candidate_name_filter %}&candidate_name={{ candidate_name_filter }}{% endif %}">First</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}{% if type_filter %}&type={{ type_filter }}{% endif %}{% if candidate_name_filter %}&candidate_name={{ candidate_name_filter }}{% endif %}">Previous</a>
                        </li>
                    {% endif %}

                    <li class="page-item active">
                        <span class="page-link">{{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
                    </li>

                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}{% if type_filter %}&type={{ type_filter }}{% endif %}{% if candidate_name_filter %}&candidate_name={{ candidate_name_filter }}{% endif %}">Next</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}{% if type_filter %}&type={{ type_filter }}{% endif %}{% if candidate_name_filter %}&candidate_name={{ candidate_name_filter }}{% endif %}">Last</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        {% endif %}
    {% else %}
        <div class="text-center py-5 card shadow-sm">
            <div class="card-body">
                <i class="fas fa-calendar-alt fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3>{% trans "No interviews or meetings found" %}</h3>
                <p class="text-muted">{% trans "Create your first interview or adjust your filters." %}</p>
                {% comment %} <div class="btn-group mt-3" role="group">
                    <a href="{% url 'create_remote_meeting' %}" class="btn btn-main-action">
                        <i class="fas fa-globe me-1"></i> {% trans "Create Remote" %}
                    </a>
                    <a href="{% url 'create_onsite_meeting' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-building me-1"></i> {% trans "Create Onsite" %}
                    </a>
                </div> {% endcomment %}
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}