@extends('layouts.app')

@section('title', 'Automações')
@section('page-title', 'Automações')

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

    <div class="mb-6 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <p class="text-sm text-slate-600">Configure uma vez. O ImóvelZapAi trabalha por você.</p>
        <div class="flex flex-wrap gap-2">
            <a href="{{ route('automation.templates.index') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Modelos</a>
            @if (auth()->user()->hasAnyRole(['Admin', 'Gestor']))
                <a href="{{ route('automation.create') }}" class="rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-700">Nova automação</a>
            @endif
        </div>
    </div>

    <div class="mb-6 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
        @foreach ([
            'workflows_total' => 'Automações',
            'workflows_active' => 'Ativas',
            'executions_succeeded' => 'Execuções concluídas',
            'executions_failed' => 'Execuções com falha',
            'executions_skipped' => 'Execuções ignoradas',
            'executions_last_24h' => 'Últimas 24h',
        ] as $key => $label)
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                <p class="text-xs uppercase tracking-wide text-slate-500">{{ $label }}</p>
                <p class="mt-1 text-xl font-semibold text-slate-900">{{ $dashboard[$key] ?? 0 }}</p>
            </div>
        @endforeach
    </div>

    <div class="overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
        <table class="min-w-full divide-y divide-slate-200 text-sm">
            <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                <tr>
                    <th class="px-4 py-3">Nome</th>
                    <th class="px-4 py-3">Gatilho</th>
                    <th class="px-4 py-3">Status</th>
                    <th class="px-4 py-3">Execuções</th>
                    <th class="px-4 py-3"></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                @forelse ($workflows as $workflow)
                    <tr>
                        <td class="px-4 py-3 font-medium">{{ $workflow->name }}</td>
                        <td class="px-4 py-3 text-slate-600">{{ $workflow->triggers->first()?->event_key ?? '—' }}</td>
                        <td class="px-4 py-3">{{ $workflow->status->label() }}</td>
                        <td class="px-4 py-3">{{ $workflow->executions_count ?? 0 }}</td>
                        <td class="px-4 py-3 text-right">
                            <a href="{{ route('automation.show', $workflow->id) }}" class="font-medium text-emerald-700">Detalhes</a>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="px-4 py-8 text-center text-slate-500">Nenhuma automação configurada.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="mt-4">{{ $workflows->links() }}</div>
@endsection
