{% extends 'layout.twig' %}

{% block title %}Policies — IRDB{% endblock %}

{% block content %}
{% import 'partials/sort.twig' as sort %}
<div class="mx-auto max-w-5xl">
    <div class="flex items-center justify-between">
        <h1 class="text-2xl font-semibold tracking-tight">Policies</h1>
        <span class="text-sm text-slate-500 dark:text-slate-400">{{ list.total|default(0) }} total</span>
    </div>

    {% if can_write %}
        <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-800 dark:bg-slate-900">
            <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">New policy</h2>
            <form method="post" action="/app/policies" class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-3 text-sm">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <div>
                    <label for="p-name" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Name</label>
                    <input type="text" id="p-name" name="name" required
                           class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 font-mono dark:border-slate-700 dark:bg-slate-950">
                </div>
                <div class="md:col-span-2">
                    <label for="p-desc" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Description</label>
                    <input type="text" id="p-desc" name="description"
                           class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 dark:border-slate-700 dark:bg-slate-950">
                </div>
                <div class="md:col-span-3 flex items-center justify-between">
                    <label class="flex items-center gap-2 text-xs text-slate-600 dark:text-slate-400">
                        <input type="checkbox" name="include_manual_blocks" value="1" checked> include manual blocks
                    </label>
                    <button type="submit" class="rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-indigo-500">Create</button>
                </div>
                <p class="md:col-span-3 text-xs text-slate-400">Thresholds are configured on the policy edit page after creation.</p>
            </form>
        </section>
    {% endif %}

    <section class="mt-6 overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900">
        <table class="w-full text-sm" data-sortable-table="policies-index">
            <thead class="border-b border-slate-200 bg-slate-50 text-left text-xs uppercase tracking-wider text-slate-500 dark:border-slate-800 dark:bg-slate-950 dark:text-slate-400">
                <tr>
                    {{ sort.th('Name', 'name') }}
                    {{ sort.th('Description', 'description') }}
                    {{ sort.th('Categories', 'categories', 'number', 'right') }}
                    {{ sort.th('Manual blocks', 'manual_blocks') }}
                    {% if can_write %}<th class="px-4 py-2 text-right font-medium">Actions</th>{% endif %}
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                {% for p in list.items|default([]) %}
                    <tr>
                        <td class="px-4 py-2" data-sort-value="{{ p.name }}"><a href="/app/policies/{{ p.id }}" class="font-mono text-indigo-600 hover:underline dark:text-indigo-400">{{ p.name }}</a></td>
                        <td class="px-4 py-2 text-slate-600 dark:text-slate-300" data-sort-value="{{ p.description|default('') }}">{{ p.description|default('—') }}</td>
                        <td class="px-4 py-2 text-right font-mono" data-sort-value="{{ p.thresholds|length }}">{{ p.thresholds|length }}</td>
                        <td class="px-4 py-2 text-slate-500 dark:text-slate-400" data-sort-value="{{ p.include_manual_blocks ? 'yes' : 'no' }}">{{ p.include_manual_blocks ? 'yes' : 'no' }}</td>
                        {% if can_write %}
                            <td class="px-4 py-2 text-right">
                                <a href="/app/policies/{{ p.id }}" class="mr-2 text-xs text-indigo-600 hover:underline dark:text-indigo-400">Edit</a>
                                {% include 'partials/confirm_form.twig' with {
                                    action: '/app/policies/' ~ p.id ~ '/delete',
                                    label: 'Delete',
                                    description: 'Delete this policy. Refused if any consumer references it.',
                                } only %}
                            </td>
                        {% endif %}
                    </tr>
                {% else %}
                    <tr><td colspan="5" class="px-4 py-6 text-center text-slate-400">No policies.</td></tr>
                {% endfor %}
            </tbody>
        </table>
    </section>
</div>
{% endblock %}
