@extends('layouts.app')

@section('content')
<div class="py-8">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-6">

        <div class="flex items-center justify-between">
            <h1 class="text-2xl font-bold text-gray-800">Webhook Mappings</h1>
            <a href="{{ route('admin.mappings.create') }}" class="inline-flex items-center gap-1 px-4 py-2 bg-indigo-600 text-white text-sm font-medium rounded-lg hover:bg-indigo-700">+ New Mapping</a>
        </div>

        @if(session('success'))<div class="bg-green-50 border-l-4 border-green-500 text-green-700 px-4 py-3 rounded">{{ session('success') }}</div>@endif
        @if(session('error'))<div class="bg-red-50 border-l-4 border-red-500 text-red-700 px-4 py-3 rounded">{{ session('error') }}</div>@endif

        <div class="bg-white rounded-xl border border-gray-200 overflow-hidden">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 text-xs uppercase tracking-wider text-gray-500">
                    <tr>
                        <th class="px-5 py-3 text-left">Name</th>
                        <th class="px-5 py-3 text-left">Prefix</th>
                        <th class="px-5 py-3 text-left">Provider</th>
                        <th class="px-5 py-3 text-left">Forward URL</th>
                        <th class="px-5 py-3 text-left">Auth</th>
                        <th class="px-5 py-3 text-center">Status</th>
                        <th class="px-5 py-3 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($mappings as $m)
                        <tr class="hover:bg-gray-50">
                            <td class="px-5 py-3 font-medium text-gray-800">{{ $m->name }}</td>
                            <td class="px-5 py-3"><code class="bg-gray-100 px-1.5 py-0.5 rounded text-xs">{{ $m->prefix }}</code></td>
                            <td class="px-5 py-3 uppercase text-xs text-gray-600">{{ $m->provider }}</td>
                            <td class="px-5 py-3 text-xs text-gray-500 truncate max-w-xs" title="{{ $m->forward_url }}">{{ $m->forward_url }}</td>
                            <td class="px-5 py-3 text-xs">{{ $m->auth_type }}</td>
                            <td class="px-5 py-3 text-center">
                                @if($m->is_active)
                                    <span class="text-xs px-2 py-0.5 bg-green-100 text-green-700 rounded">active</span>
                                @else
                                    <span class="text-xs px-2 py-0.5 bg-gray-100 text-gray-600 rounded">inactive</span>
                                @endif
                            </td>
                            <td class="px-5 py-3 text-right">
                                <form method="POST" action="{{ route('admin.mappings.test-forward', $m) }}" class="inline">@csrf
                                    <button class="text-xs text-blue-600 hover:underline" onclick="return confirm('Send test payload to {{ $m->forward_url }}?')">Test</button>
                                </form>
                                <a href="{{ route('admin.mappings.edit', $m) }}" class="text-xs text-indigo-600 hover:underline ms-2">Edit</a>
                                <form method="POST" action="{{ route('admin.mappings.destroy', $m) }}" class="inline ms-2">@csrf @method('DELETE')
                                    <button class="text-xs text-red-600 hover:underline" onclick="return confirm('Delete mapping {{ $m->prefix }}?')">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="px-5 py-12 text-center text-gray-400">Belum ada mapping. <a href="{{ route('admin.mappings.create') }}" class="text-indigo-600 underline">Buat baru</a></td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{ $mappings->links() }}
    </div>
</div>
@endsection
