{# app/templates/admin/code-quality-violations.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% 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 %}

{% block title %}Violations List{% endblock %}

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

{% block extra_scripts %}
<script defer src="{{ url_for('dev_tools_static', path='admin/js/code-quality-violations.js') }}"></script>
{% endblock %}

{% block content %}
{{ page_header('Code Quality Violations', subtitle='Review and manage violations across all validators', back_url='/admin/code-quality', back_label='Back to Dashboard') }}

{{ loading_state('Loading violations...') }}

{{ error_state('Error loading violations') }}

<!-- Content -->
<div x-show="!loading">
    <!-- Filters -->
    <div class="mb-6 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">Filters</h3>
        <div class="grid gap-4 md:grid-cols-5">
            <!-- Validator Type Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                    Validator
                </label>
                <select x-model="filters.validator_type"
                        @change="applyFilters()"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-select rounded-md">
                    <option value="">All Validators</option>
                    <option value="architecture">Architecture</option>
                    <option value="security">Security</option>
                    <option value="performance">Performance</option>
                </select>
            </div>

            <!-- Severity Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                    Severity
                </label>
                <select x-model="filters.severity"
                        @change="applyFilters()"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-select rounded-md">
                    <option value="">All</option>
                    <option value="error">Error</option>
                    <option value="warning">Warning</option>
                    <option value="info">Info</option>
                </select>
            </div>

            <!-- Status Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                    Status
                </label>
                <select x-model="filters.status"
                        @change="applyFilters()"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-select rounded-md">
                    <option value="">All</option>
                    <option value="open">Open</option>
                    <option value="assigned">Assigned</option>
                    <option value="resolved">Resolved</option>
                    <option value="ignored">Ignored</option>
                </select>
            </div>

            <!-- Rule ID Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                    Rule ID
                </label>
                <input x-model="filters.rule_id"
                       @input.debounce.500ms="applyFilters()"
                       type="text"
                       placeholder="e.g. SEC-001"
                       class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input rounded-md">
            </div>

            <!-- File Path Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-400 mb-2">
                    File Path
                </label>
                <input x-model="filters.file_path"
                       @input.debounce.500ms="applyFilters()"
                       type="text"
                       placeholder="e.g. app/api"
                       class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input rounded-md">
            </div>
        </div>
    </div>

    <!-- Violations Table -->
    {% call table_wrapper() %}
                {{ table_header(['Validator', 'Rule', 'Severity', 'File', 'Line', 'Message', 'Status', 'Actions']) }}
                <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                    <template x-if="violations.length === 0">
                        <tr>
                            <td colspan="8" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                                No violations found
                            </td>
                        </tr>
                    </template>
                    <template x-for="violation in violations" :key="violation.id">
                        <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700">
                            <!-- Validator Type Badge -->
                            <td class="px-4 py-3 text-xs">
                                <span class="px-2 py-1 font-semibold leading-tight rounded-full capitalize"
                                      :class="{
                                          'text-purple-700 bg-purple-100 dark:bg-purple-700 dark:text-purple-100': violation.validator_type === 'architecture',
                                          'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100': violation.validator_type === 'security',
                                          'text-yellow-700 bg-yellow-100 dark:bg-yellow-700 dark:text-yellow-100': violation.validator_type === 'performance'
                                      }"
                                      x-text="violation.validator_type">
                                </span>
                            </td>

                            <!-- Rule ID -->
                            <td class="px-4 py-3">
                                <div class="flex items-center text-sm">
                                    <div>
                                        <p class="font-semibold" x-text="violation.rule_id"></p>
                                    </div>
                                </div>
                            </td>

                            <!-- Severity Badge -->
                            <td class="px-4 py-3 text-xs">
                                <span class="px-2 py-1 font-semibold leading-tight rounded-full"
                                      :class="{
                                          'text-red-700 bg-red-100 dark:bg-red-700 dark:text-red-100': violation.severity === 'error',
                                          'text-yellow-700 bg-yellow-100 dark:text-yellow-100 dark:bg-yellow-700': violation.severity === 'warning',
                                          'text-blue-700 bg-blue-100 dark:text-blue-100 dark:bg-blue-700': violation.severity === 'info'
                                      }"
                                      x-text="violation.severity">
                                </span>
                            </td>

                            <!-- File Path -->
                            <td class="px-4 py-3 text-sm">
                                <p class="truncate max-w-xs" :title="violation.file_path" x-text="violation.file_path"></p>
                            </td>

                            <!-- Line Number -->
                            <td class="px-4 py-3 text-sm">
                                <p x-text="violation.line_number"></p>
                            </td>

                            <!-- Message -->
                            <td class="px-4 py-3 text-sm">
                                <p class="truncate max-w-md" :title="violation.message" x-text="violation.message"></p>
                            </td>

                            <!-- Status Badge -->
                            <td class="px-4 py-3 text-xs">
                                <span class="px-2 py-1 font-semibold leading-tight rounded-full"
                                      :class="{
                                          'text-gray-700 bg-gray-100 dark:text-gray-100 dark:bg-gray-700': violation.status === 'open',
                                          'text-blue-700 bg-blue-100 dark:text-blue-100 dark:bg-blue-700': violation.status === 'assigned',
                                          'text-green-700 bg-green-100 dark:text-green-100 dark:bg-green-700': violation.status === 'resolved',
                                          'text-orange-700 bg-orange-100 dark:text-orange-100 dark:bg-orange-700': violation.status === 'ignored'
                                      }"
                                      x-text="violation.status">
                                </span>
                            </td>

                            <!-- Actions -->
                            <td class="px-4 py-3">
                                <div class="flex items-center space-x-2">
                                    <a :href="'/admin/code-quality/violations/' + violation.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>
                                </div>
                            </td>
                        </tr>
                    </template>
                </tbody>
    {% endcall %}

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