@extends('admin.layouts.app')

@section('title', 'Matches')
@section('page-title', '⚽ Matches')

@section('header-actions')
    <a href="{{ route('admin.matches.create') }}"
       class="bg-wc-green text-white text-sm font-bold px-4 py-2 rounded-xl hover:bg-wc-green-dark transition-colors">
        + Add Match
    </a>
@endsection

@section('content')

{{-- Status filter tabs --}}
<div class="flex gap-2 mb-6">
    @foreach(['All', 'Upcoming', 'Live', 'Finished'] as $tab)
        @php
            $status = strtolower($tab);
            $active = request('status', 'all') === $status;
        @endphp
        <a href="{{ route('admin.matches.index', $tab !== 'All' ? ['status' => $status] : []) }}"
           class="px-4 py-2 rounded-xl text-sm font-semibold transition-colors
                  {{ $active
                      ? 'bg-wc-green text-white'
                      : 'bg-white text-gray-600 border border-gray-200 hover:border-wc-green hover:text-wc-green' }}">
            {{ $tab }}
            @if($tab === 'Live')
                <span class="ml-1 w-2 h-2 rounded-full bg-red-500 inline-block animate-pulse"></span>
            @endif
        </a>
    @endforeach
</div>

<div class="bg-white rounded-2xl border border-gray-200 shadow-sm overflow-hidden">
    <table class="w-full text-sm">
        <thead class="bg-gray-50 border-b border-gray-200">
            <tr>
                <th class="px-6 py-3 text-left font-semibold text-gray-600">#</th>
                <th class="px-6 py-3 text-left font-semibold text-gray-600">Match</th>
                <th class="px-6 py-3 text-left font-semibold text-gray-600">
                    Date & Time
                    <span class="text-gray-400 font-normal">({{ str_replace('_', ' ', config('app.admin_timezone')) }})</span>
                </th>
                <th class="px-6 py-3 text-left font-semibold text-gray-600">Stage</th>
                <th class="px-6 py-3 text-left font-semibold text-gray-600">Status</th>
                <th class="px-6 py-3 text-left font-semibold text-gray-600">Score</th>
                <th class="px-6 py-3 text-right font-semibold text-gray-600">Actions</th>
        </thead>
        <tbody class="divide-y divide-gray-100">
            @forelse($matches as $match)
            <tr class="hover:bg-gray-50 transition-colors">
                <td class="px-6 py-4 text-gray-400 font-mono text-xs">{{ $match->id }}</td>
                <td class="px-6 py-4">
                    <div class="font-semibold text-gray-800">
                        {{ $match->homeTeam->name }}
                        <span class="text-gray-400 font-normal mx-1">vs</span>
                        {{ $match->awayTeam->name }}
                    </div>
                    <div class="text-xs text-gray-400 mt-0.5">
                        {{ $match->homeTeam->code }} — {{ $match->awayTeam->code }}
                        @if($match->group_name)
                            · Group {{ $match->group_name }}
                        @endif
                    </div>
                </td>
                <td class="px-6 py-4 text-gray-600">
                    {{ $match->match_datetime_admin->format('d M Y') }}<br>
                    <span class="text-gray-400 text-xs">{{ $match->match_datetime_admin->format('H:i') }}</span>
                </td>
                <td class="px-6 py-4">
                    <span class="bg-gray-100 text-gray-600 text-xs px-2 py-1 rounded-full font-medium">
                        {{ ucfirst(str_replace('_', ' ', $match->stage)) }}
                    </span>
                </td>
                <td class="px-6 py-4">
                    @php
                        $colors = [
                            'upcoming' => 'bg-blue-100 text-blue-700',
                            'live'     => 'bg-red-100 text-red-700 animate-pulse',
                            'finished' => 'bg-gray-100 text-gray-600',
                        ];
                    @endphp
                    <span class="text-xs px-2.5 py-1 rounded-full font-bold {{ $colors[$match->status] }}">
                        {{ strtoupper($match->status) }}
                    </span>
                </td>
                <td class="px-6 py-4 font-bold text-gray-800">
                    {{ $match->home_score !== null
                        ? $match->home_score . ' – ' . $match->away_score
                        : '—' }}
                </td>
                <td class="px-6 py-4 text-right">
                    <div class="flex items-center justify-end gap-2">
                        <a href="{{ route('admin.matches.edit', $match->id) }}"
                           class="inline-flex items-center gap-1 text-xs bg-wc-green/10 text-wc-green
                                  font-semibold px-3 py-1.5 rounded-lg hover:bg-wc-green/20 transition-colors">
                            ✏️ Edit
                        </a>
                        @if($match->status === 'upcoming')
                        <form method="POST" action="{{ route('admin.matches.destroy', $match->id) }}"
                              onsubmit="confirmDelete(event, this)">
                            @csrf @method('DELETE')
                            <button type="submit"
                                    class="text-xs bg-red-50 text-red-600 hover:bg-red-100
                                           px-3 py-1.5 rounded-lg font-semibold transition-colors">
                                🗑 Delete
                            </button>
                        </form>
                        @endif
                    </div>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="7" class="px-6 py-16 text-center text-gray-400">
                    <div class="text-4xl mb-3">⚽</div>
                    <p class="font-medium">No matches yet.</p>
                    <a href="{{ route('admin.matches.create') }}"
                       class="text-wc-green font-semibold hover:underline mt-1 inline-block">
                        Create the first match →
                    </a>
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>

    @if($matches->hasPages())
    <div class="px-6 py-4 border-t border-gray-100">
        {{ $matches->links() }}
    </div>
    @endif
</div>
@endsection
