<?php

use App\Models\Participant;
use App\Services\MatcherService;
use Illuminate\Database\Eloquent\Collection;
use function Livewire\Volt\{state, mount};

state(['participants' => Collection::class,
    'render' => false]);
state(['password']);

mount(function ($password) {
    $this->password = $password;
    if ($this->password  == env('ADMIN_PASSWORD')) {
        $this->render = true;
    }
    $this->participants = Participant::all();
});

$runMatch = function () {
    $matcher = app(MatcherService::class);
    $matcher->match();

    session()->flash('match-message',"Done");

        $this->participants = Participant::all();
};
?>

<div class="p-6 bg-gray-100 min-h-screen">
    @if($render)
    <div class="max-w-7xl mx-auto">

        <div class="flex justify-between items-center mb-6">
            <h1 class="text-3xl font-extrabold text-gray-900">All Participants Data</h1>

            <button
                wire:click="runMatch"
                wire:loading.attr="disabled"
                wire:target="runMatch"
                class="px-4 py-2 bg-indigo-600 text-white font-semibold rounded-lg shadow-md hover:bg-indigo-700 transition duration-150 disabled:opacity-50"
            >
                <span wire:loading wire:target="runMatch">Processing Match...</span>
                <span wire:loading.remove wire:target="runMatch">Run Matcher Service</span>
            </button>
        </div>

        <p class="text-gray-600 mb-4">
            Displaying all records from the `participants` table.
        </p>

        @if (session()->has('match-message'))
            <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-4 rounded" role="alert">
                <p class="font-bold">Success!</p>
                <p>{{ session('match-message') }}</p>
            </div>
        @endif

        @if($participants->isEmpty())
            <div class="p-4 text-center text-gray-500 bg-white shadow rounded-lg">
                No records
            </div>
        @else
            <div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Giving To (ID)</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Prompt</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Submission URL</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Desperate?</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Token (ULID)</th>
                    </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                    @foreach($participants as $participant)
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{{ $participant->id }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $participant->user_id }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $participant->giving_id ?? 'N/A' }}</td>

                            <td class="px-6 py-4 text-sm text-gray-500 max-w-xs truncate" title="{{ $participant->prompt }}">
                                {{ Str::limit($participant->prompt, 50) }}
                            </td>

                            <td class="px-6 py-4 whitespace-nowrap text-sm text-blue-600 hover:underline">
                                @if($participant->submission_url)
                                    <a href="{{ $participant->submission_url }}" target="_blank">View Link</a>
                                @else
                                    N/A
                                @endif
                            </td>

                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full
                                {{ $participant->is_desperate ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800' }}">
                                {{ $participant->is_desperate ? 'YES' : 'No' }}
                            </span>
                            </td>

                            <td class="px-6 py-4 text-sm font-mono text-gray-500 truncate" title="{{ $participant->token }}">
                                {{ $participant->token }}
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        @endif
        @endif
    </div>
