{% extends "admin/base.html" %}
{% from 'shared/macros/headers.html' import page_header %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/tables.html' import table_wrapper, table_header, table_empty_state %}
{% from 'shared/macros/pagination.html' import pagination %}

{% block title %}Leads{% endblock %}

{% block alpine_data %}leadsList(){% endblock %}

{% block content %}
{{ page_header('Leads', action_label='Export CSV', action_onclick='exportCSV()', action_icon='download') }}

{{ loading_state('Loading leads...') }}
{{ error_state('Error loading leads') }}

<!-- Filters -->
<div x-show="!loading && !error" class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
        <!-- Min Score -->
        <div class="flex items-center gap-3">
            <label class="text-sm font-medium text-gray-700 dark:text-gray-400 whitespace-nowrap">Min Score</label>
            <input type="number" x-model.number="minScore" @change="pagination.page = 1; loadLeads()" min="0" max="100" {# noqa: FE008 - score filter, not a quantity stepper #}
                   class="w-24 px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray dark:bg-gray-700 dark:text-gray-300">
        </div>

        <!-- Filters -->
        <div class="flex flex-wrap gap-3">
            <select x-model="filterTier" @change="pagination.page = 1; loadLeads()"
                    class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none">
                <option value="">All Tiers</option>
                <option value="top_priority">Top Priority</option>
                <option value="quick_win">Quick Win</option>
                <option value="strategic">Strategic</option>
            </select>

            <select x-model="filterChannel" @change="pagination.page = 1; loadLeads()"
                    class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none">
                <option value="">All Channels</option>
                <option value="digital">Digital</option>
                <option value="offline">Offline</option>
            </select>

            <select x-model="filterIssue" @change="pagination.page = 1; loadLeads()"
                    class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none">
                <option value="">All Issues</option>
                <option value="no_ssl">No SSL</option>
                <option value="very_slow">Very Slow</option>
                <option value="not_mobile_friendly">Not Mobile Friendly</option>
                <option value="outdated_cms">Outdated CMS</option>
                <option value="no_website">No Website</option>
                <option value="uses_gmail">Uses Gmail</option>
            </select>

            <select x-model="filterHasEmail" @change="pagination.page = 1; loadLeads()"
                    class="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none">
                <option value="">Has Email</option>
                <option value="true">Yes</option>
                <option value="false">No</option>
            </select>
        </div>
    </div>
</div>

<!-- Leads Table -->
<div x-show="!loading && !error">
    {% call table_wrapper() %}
        {{ table_header(['Business / Domain', 'Score', 'Tier', 'Issues', 'Contact', 'Actions']) }}
        <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
            {{ table_empty_state(6, title='No leads found', x_message="filterTier || filterChannel || filterIssue || filterHasEmail || minScore > 0 ? 'Try adjusting your filters' : 'No scored leads yet'", show_condition='leads.length === 0', icon='user-group') }}
            <template x-for="lead in leads" :key="lead.id">
                <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
                    <!-- Business / Domain with Avatar -->
                    <td class="px-4 py-3">
                        <div class="flex items-center text-sm">
                            <div class="relative hidden w-8 h-8 mr-3 rounded-full md:block">
                                <div class="absolute inset-0 rounded-full bg-purple-100 dark:bg-purple-600 flex items-center justify-center">
                                    <span class="text-xs font-semibold text-purple-600 dark:text-purple-100"
                                          x-text="(lead.business_name || lead.domain_name)?.charAt(0).toUpperCase() || '?'"></span>
                                </div>
                            </div>
                            <div>
                                <p class="font-semibold" x-text="lead.business_name || lead.domain_name"></p>
                                <p class="text-xs text-gray-600 dark:text-gray-400" x-show="lead.domain_name && lead.business_name" x-text="lead.domain_name"></p>
                            </div>
                        </div>
                    </td>

                    <!-- Score -->
                    <td class="px-4 py-3">
                        <span class="text-lg font-bold" :class="scoreColor(lead.score)"
                              x-text="lead.score ?? '—'"></span>
                    </td>

                    <!-- Tier Badge -->
                    <td class="px-4 py-3">
                        <span class="px-2.5 py-0.5 text-xs font-medium rounded-full"
                              :class="tierBadgeClass(lead.lead_tier)"
                              x-text="lead.lead_tier?.replace('_', ' ')"></span>
                    </td>

                    <!-- Issues -->
                    <td class="px-4 py-3">
                        <div class="flex flex-wrap gap-1">
                            <template x-for="flag in (lead.reason_flags || []).slice(0, 3)" :key="flag">
                                <span class="px-1.5 py-0.5 text-xs bg-red-100 text-red-700 rounded dark:bg-red-900 dark:text-red-300"
                                      x-text="flag.replace('_', ' ')"></span>
                            </template>
                        </div>
                    </td>

                    <!-- Contact -->
                    <td class="px-4 py-3 text-sm">
                        <span x-text="lead.primary_email || lead.primary_phone || '—'" class="text-xs"></span>
                    </td>

                    <!-- Actions -->
                    <td class="px-4 py-3">
                        <div class="flex items-center space-x-2 text-sm">
                            <a :href="'/admin/prospecting/prospects/' + lead.id"
                               class="flex items-center justify-center p-2 text-purple-600 rounded-lg hover:bg-purple-50 dark:text-purple-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                               title="View details">
                                <span x-html="$icon('eye', 'w-5 h-5')"></span>
                            </a>
                            <button @click="sendCampaign(lead)"
                                    class="flex items-center justify-center p-2 text-green-600 rounded-lg hover:bg-green-50 dark:text-green-400 dark:hover:bg-gray-700 focus:outline-none transition-colors"
                                    title="Send campaign">
                                <span x-html="$icon('mail', 'w-5 h-5')"></span>
                            </button>
                        </div>
                    </td>
                </tr>
            </template>
        </tbody>
    {% endcall %}

    {{ pagination() }}
</div>
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('prospecting_static', path='admin/js/leads.js') }}"></script>
{% endblock %}
