<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Event;
use App\Models\EventRegistration;
use Illuminate\Http\Request;
use Illuminate\View\View;

class EventRegistrationController extends Controller
{
    public function index(Request $request): View
    {
        $query = EventRegistration::query()
            ->with('event')
            ->when($request->event_id, fn ($q) => $q->where('event_id', $request->event_id))
            ->when($request->status, fn ($q) => $q->where('status', $request->status))
            ->when($request->search, function ($q) use ($request) {
                $search = (string) $request->search;
                $q->where(function ($q2) use ($search) {
                    $q2->where('full_name', 'like', "%{$search}%")
                        ->orWhere('phone', 'like', "%{$search}%")
                        ->orWhere('email', 'like', "%{$search}%");
                });
            })
            ->latest();

        $registrations = $query->paginate(20)->withQueryString();
        $events = Event::query()->ordered()->get(['id', 'title']);

        return view('admin.event-registrations.index', compact('registrations', 'events'));
    }
}
