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

{% block title %}{% trans "Scheduled Interviews List" %} - {{ block.super }}{% endblock %}

{% block customCSS %}
{# (Your existing CSS is kept here, as it is perfect for the theme) #}
<style>
    /* ... (Your CSS styles) ... */
</style>
{% endblock %}

{% block content %}
{{interviews}}
<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 "Scheduled Interviews" %}
        </h1>
        {# FIX: Using safe anchor href="#" to prevent the NoReverseMatch crash. #}
        {# Replace '#' with {% url 'create_scheduled_interview' %} once the URL name is defined in urls.py #}
        <a href="#" class="btn btn-main-action">
            <i class="fas fa-plus me-1"></i> {% trans "Schedule Interview" %}
        </a>
    </div>

    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <form method="GET" class="row g-3 align-items-end">
                {# Search field #}
                <div class="col-md-4">
                    <label for="q" class="form-label small text-muted">{% trans "Search (Candidate/Job)" %}</label>
                    <div class="input-group">
                        <input type="text" class="form-control form-control-sm" id="q" name="q" placeholder="{% trans 'Search...' %}" value="{{ search_query }}">
                    </div>
                </div>

                {# Filter by Status #}
                <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>
                        <option value="scheduled" {% if status_filter == 'scheduled' %}selected{% endif %}>{% trans "Scheduled" %}</option>
                        <option value="confirmed" {% if status_filter == 'confirmed' %}selected{% endif %}>{% trans "Confirmed" %}</option>
                        <option value="completed" {% if status_filter == 'completed' %}selected{% endif %}>{% trans "Completed" %}</option>
                        <option value="cancelled" {% if status_filter == 'cancelled' %}selected{% endif %}>{% trans "Cancelled" %}</option>
                    </select>
                </div>
                
                {# Filter by Interview Type (ONSITE/REMOTE) - This list now correctly populated #}
                <div class="col-md-3">
                    <label for="interview_type" class="form-label small text-muted">{% trans "Interview Type" %}</label>
                    <select name="interview_type" id="interview_type" class="form-select form-select-sm">
                        <option value="">{% trans "All Types" %}</option>
                        {% for type_value, type_label in interview_types %}
                            <option value="{{ type_value }}" {% if type_filter == type_value %}selected{% endif %}>
                                {{ type_label }}
                            </option>
                        {% endfor %}
                    </select>
                </div>

                <div class="col-md-2">
                    <div class="filter-buttons">
                        <button type="submit" class="btn btn-main-action btn-sm">
                            <i class="fas fa-filter me-1"></i> {% trans "Apply" %}
                        </button>
                        {% if status_filter or search_query or type_filter %}
                            {# Assuming 'interview_list' is the URL name for this view #}
                            <a href="{% url 'interview_list' %}" class="btn btn-outline-secondary btn-sm">
                                <i class="fas fa-times me-1"></i> {% trans "Clear" %}
                            </a>
                        {% endif %}
                    </div>
                </div>
            </form>
        </div>
    </div>
    {{meetings}}
    {# Using 'meetings' based on the context_object_name provided #}
    {% if meetings %}
        <div id="meetings-list">
            {# View Switcher (kept the name for simplicity) #}
            {% include "includes/_list_view_switcher.html" with list_id="meetings-list" %}

            {# Card View #}
            <div class="card-view active row">
                {% for interview in meetings %}
                    <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="{% url 'candidate_detail' interview.candidate.slug %}" class="text-decoration-none text-primary-theme">{{ interview.candidate.name }}</a>
                                    </h5>
                                    <span class="status-badge bg-{{ interview.status }}">
                                        {{ interview.status|title }}
                                    </span>
                                </div>

                                <p class="card-text text-muted small mb-3">
                                    <i class="fas fa-briefcase"></i> {% trans "Job" %}: 
                                    <a class="text-secondary text-decoration-none" href="{% url 'job_detail' interview.job.slug %}">{{ interview.job.title }}</a><br>
                                    
                                    {# --- Remote/Onsite Logic - Handles both cases safely --- #}
                                    <i class="fas {% if interview.schedule.interview_type == 'Remote' %}fa-globe{% else %}fa-map-marker-alt{% endif %}"></i> 
                                    {% trans "Type" %}: {{ interview.schedule.get_interview_type_display }}
                                    {% if interview.schedule.interview_type == 'Remote' %}<br>
                                        {# CRITICAL FIX: Safe access to zoom_meeting details #}
                                        <i class="fas fa-hashtag"></i> {% trans "Zoom ID" %}: {{ interview.zoom_meeting.meeting_id|default:"N/A" }}
                                    {% else %}<br>
                                        <i class="fas fa-building"></i> {% trans "Location" %}: {{ interview.schedule.location }}
                                    {% endif %}<br>
                                    
                                    <i class="fas fa-clock"></i> {% trans "Date" %}: {{ interview.interview_date|date:"M d, Y" }}<br>
                                    <i class="fas fa-clock"></i> {% trans "Time" %}: {{ interview.interview_time|time:"H:i" }}<br>
                                    <i class="fas fa-stopwatch"></i> {% trans "Duration" %}: {{ interview.schedule.interview_duration }} minutes
                                </p>

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

                                        {# CRITICAL FIX: Safe access to join URL #}
                                        {% if interview.schedule.interview_type == 'Remote' and interview.zoom_meeting and interview.zoom_meeting.join_url %}
                                            <a href="{{ interview.zoom_meeting.join_url }}" target="_blank" class="btn btn-sm btn-main-action">
                                                <i class="fas fa-link"></i> {% trans "Join" %}
                                            </a>
                                        {% endif %}

                                        <a href="{% url 'update_scheduled_interview' interview.slug %}" 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="{% url 'delete_scheduled_interview' interview.slug %}"
                                                hx-target="#deleteModalBody"
                                                hx-swap="outerHTML"
                                                data-item-name="{{ interview.candidate.name }} Interview">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>

            {# Table View (Logic is identical, safe access applied) #}
            <div class="table-view">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col">{% trans "Candidate" %}</th>
                                <th scope="col">{% trans "Job" %}</th>
                                <th scope="col">{% trans "Type" %}</th>
                                <th scope="col">{% trans "Date/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 interview in meetings %}
                            <tr>
                                <td>
                                    <strong class="text-primary-theme">
                                        <a href="{% url 'candidate_detail' interview.candidate.slug %}" class="text-decoration-none text-primary-theme">{{ interview.candidate.name }}</a>
                                    </strong>
                                </td>
                                <td>
                                    <a class="text-secondary text-decoration-none" href="{% url 'job_detail' interview.job.slug %}">{{ interview.job.title }}</a>
                                </td>
                                <td>
                                    {{ interview.schedule.get_interview_type_display }}
                                </td>
                                <td>{{ interview.interview_date|date:"M d, Y" }} <br>({{ interview.interview_time|time:"H:i" }})</td>
                                <td>{{ interview.schedule.interview_duration }} min</td>
                                <td>
                                    <span class="badge bg-{{ interview.status }}">
                                        {% if interview.status == 'confirmed' %}
                                            <i class="fas fa-circle me-1 text-white"></i>
                                        {% endif %}
                                        {{ interview.status|title }}
                                    </span>
                                </td>
                                <td class="text-end">
                                    <div class="btn-group btn-group-sm" role="group">
                                        
                                        {# CRITICAL FIX: Safe access to join URL #}
                                        {% if interview.schedule.interview_type == 'Remote' and interview.zoom_meeting and interview.zoom_meeting.join_url %}
                                            <a href="{{ interview.zoom_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 'scheduled_interview_detail' interview.slug %}" class="btn btn-outline-primary" title="{% trans 'View' %}">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{% url 'update_scheduled_interview' interview.slug %}" 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="#meetingModal"
                                                hx-post="{% url 'delete_scheduled_interview' interview.slug %}"
                                                hx-target="#meetingModalBody"
                                                hx-swap="outerHTML"
                                                data-item-name="{{ interview.candidate.name }} Interview">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        {# Pagination #}
        {% 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 %}&interview_type={{ type_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 %}&interview_type={{ type_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 %}&interview_type={{ type_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 %}&interview_type={{ type_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 found" %}</h3>
                <p class="text-muted">{% trans "Schedule your first interview or adjust your filters." %}</p>
                {# FIX: Using safe anchor href="#" to prevent the NoReverseMatch crash. #}
                <a href="#" class="btn btn-main-action mt-3">
                    <i class="fas fa-plus me-1"></i> {% trans "Schedule an Interview" %}
                </a>
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}