@extends('layouts.app')

@section('title', $workflow->name)
@section('page-title', $workflow->name)

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

    <div class="mb-4 flex flex-wrap items-center justify-between gap-2">
        <a href="{{ route('automation.index') }}" class="text-sm font-medium text-emerald-700">← Voltar às Automações</a>
        @if (auth()->user()->hasAnyRole(['Admin', 'Gestor']))
            <div class="flex flex-wrap gap-2">
                <a href="{{ route('automation.edit', $workflow->id) }}" class="rounded-lg border border-slate-300 px-3 py-1.5 text-sm font-semibold text-slate-700">Editar</a>
                @if ($workflow->status->value === 'active')
                    <form method="post" action="{{ route('automation.pause', $workflow->id) }}">@csrf<button class="rounded-lg border border-amber-300 px-3 py-1.5 text-sm font-semibold text-amber-800">Pausar</button></form>
                @else
                    <form method="post" action="{{ route('automation.activate', $workflow->id) }}">@csrf<button class="rounded-lg bg-emerald-600 px-3 py-1.5 text-sm font-semibold text-white">Ativar</button></form>
                @endif
            </div>
        @endif
    </div>

    <div class="mb-6 grid gap-4 lg:grid-cols-3">
        <div class="rounded-xl border border-slate-200 bg-white p-5 shadow-sm lg:col-span-1">
            <p class="text-xs uppercase text-slate-500">Status</p>
            <p class="mt-1 text-lg font-semibold">{{ $workflow->status->label() }}</p>
            <p class="mt-4 text-xs uppercase text-slate-500">Gatilho</p>
            <p class="mt-1 text-sm">{{ $workflow->triggers->first()?->event_key ?? '—' }}</p>
            <p class="mt-4 text-xs uppercase text-slate-500">Ações</p>
            <ul class="mt-1 list-inside list-disc text-sm text-slate-700">
                @forelse ($workflow->actions->sortBy('sort_order') as $action)
                    <li>
                        {{ $action->action_key instanceof \ImovelZapAi\Automation\Domain\Enums\ActionKey ? $action->action_key->label() : (string) $action->action_key }}
                        @if($action->delay_seconds > 0)
                            <span class="text-slate-500">(+{{ $action->delay_seconds }}s)</span>
                        @endif
                    </li>
                @empty
                    <li>Nenhuma ação configurada</li>
                @endforelse
            </ul>
        </div>

        <div class="rounded-xl border border-slate-200 bg-white shadow-sm lg:col-span-2">
            <div class="border-b border-slate-200 px-5 py-3">
                <h2 class="font-semibold text-slate-900">Execuções recentes</h2>
            </div>
            <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">Quando</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3">Duração</th>
                        <th class="px-4 py-3">Detalhe</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse ($executions as $execution)
                        <tr>
                            <td class="px-4 py-3">{{ $execution->created_at?->format('d/m/Y H:i') }}</td>
                            <td class="px-4 py-3">{{ $execution->status->label() }}</td>
                            <td class="px-4 py-3">{{ $execution->duration_ms !== null ? $execution->duration_ms.' ms' : '—' }}</td>
                            <td class="px-4 py-3 text-slate-600">{{ $execution->error_message ?? '—' }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="px-4 py-8 text-center text-slate-500">Nenhuma execução registrada.</td></tr>
                    @endforelse
                </tbody>
            </table>
            <div class="px-4 py-3">{{ $executions->links() }}</div>
        </div>
    </div>
@endsection
