@extends('layouts.app')

@section('title', 'Modelos de automação')
@section('page-title', 'Modelos de automação')

@section('content')
    @include('partials.flash')

    <div class="mb-6 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <a href="{{ route('automation.index') }}" class="text-sm font-medium text-emerald-700 hover:text-emerald-800">← Voltar às Automações</a>
            <p class="mt-2 text-sm text-slate-600">Instale um modelo pronto. Ele cria a automação ativa com gatilho e ações configurados.</p>
        </div>
    </div>

    <div class="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
        @forelse ($templates as $template)
            @php
                $definition = is_array($template->definition) ? $template->definition : [];
                $eventKey = $definition['trigger']['event_key'] ?? null;
                $actions = $definition['actions'] ?? [];
            @endphp
            <div class="flex flex-col rounded-xl border border-slate-200 bg-white p-5 shadow-sm">
                <p class="text-xs font-semibold uppercase tracking-wide text-emerald-700">{{ $template->category }}</p>
                <h2 class="mt-1 text-lg font-semibold text-slate-900">{{ $template->name }}</h2>
                <p class="mt-2 text-sm text-slate-600">{{ $template->description }}</p>

                <div class="mt-4 space-y-3 rounded-lg border border-slate-100 bg-slate-50 p-3 text-sm">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Quando</p>
                        <p class="mt-0.5 text-slate-800">{{ $triggerLabels[$eventKey] ?? ($eventKey ?: '—') }}</p>
                    </div>
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Então</p>
                        <ul class="mt-1 list-inside list-disc text-slate-700">
                            @forelse ($actions as $action)
                                <li>
                                    {{ $actionLabels[$action['action_key'] ?? ''] ?? ($action['action_key'] ?? 'Ação') }}
                                    @if (! empty($action['delay_seconds']))
                                        <span class="text-slate-500">(+{{ $action['delay_seconds'] }}s)</span>
                                    @endif
                                </li>
                            @empty
                                <li class="text-slate-500">Sem ações</li>
                            @endforelse
                        </ul>
                    </div>
                </div>

                <div class="mt-auto pt-4">
                    @if (auth()->user()->hasAnyRole(['Admin', 'Gestor']))
                        <form method="post" action="{{ route('automation.templates.install', $template->slug) }}">
                            @csrf
                            <button type="submit" class="w-full rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-700">
                                Instalar e ativar
                            </button>
                        </form>
                    @else
                        <p class="text-xs text-slate-500">Somente Admin ou Gestor pode instalar modelos.</p>
                    @endif
                </div>
            </div>
        @empty
            <div class="rounded-xl border border-dashed border-slate-300 bg-white p-8 text-center md:col-span-2 xl:col-span-3">
                <p class="text-slate-600">Nenhum modelo disponível no momento.</p>
                <p class="mt-1 text-sm text-slate-500">Recarregue a página após aplicar as migrations do módulo de Automações.</p>
            </div>
        @endforelse
    </div>
@endsection
