<?php

namespace App\Http\Controllers\Admin\Extras;

use App\Http\Controllers\Admin\PageController;
use App\Http\Controllers\Concerns\ManagesEventRequests;
use App\Models\Event;
use App\Services\School\EventManagementService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class EventController extends PageController
{
    use ManagesEventRequests;

    public function __construct(private readonly EventManagementService $events) {}

    public function index(Request $request): View
    {
        $events = Event::query()
            ->with(['createdBy', 'studentOrganizer', 'approvedBy'])
            ->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')))
            ->when($request->filled('event_type'), fn ($q) => $q->where('event_type', $request->string('event_type')))
            ->orderByDesc('starts_at')
            ->paginate(20)
            ->withQueryString();

        $pendingCount = Event::query()->where('status', 'pending_approval')->count();

        return $this->page('admin.events.index', [
            'crumbTitle' => 'Events & Functions',
            'crumbSub' => 'School calendar',
            'events' => $events,
            'pendingCount' => $pendingCount,
            'eventTypes' => EventManagementService::EVENT_TYPES,
        ]);
    }

    public function create(): View
    {
        return $this->page('admin.events.create', [
            'crumbTitle' => 'Events & Functions',
            'crumbSub' => 'New event',
            'eventTypes' => EventManagementService::EVENT_TYPES,
            'audiences' => EventManagementService::AUDIENCES,
            'statuses' => EventManagementService::STATUSES,
        ]);
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $this->validateEventRequest($request);
        $data['registration_required'] = $request->boolean('registration_required');
        $data['audience'] = $data['audience'] ?? 'all';
        $data['status'] = $data['status'] ?? 'scheduled';

        $event = $this->events->create(auth()->user(), $data);

        return redirect()
            ->route('extras.events.show', $event)
            ->with('success', 'Event created successfully.');
    }

    public function show(Event $event): View
    {
        $event->load(['createdBy', 'studentOrganizer', 'approvedBy']);

        return $this->page('admin.events.show', [
            'crumbTitle' => 'Events & Functions',
            'crumbSub' => $event->title,
            'event' => $event,
            'eventsService' => $this->events,
        ]);
    }

    public function edit(Event $event): View
    {
        return $this->page('admin.events.edit', [
            'crumbTitle' => 'Events & Functions',
            'crumbSub' => 'Edit event',
            'event' => $event,
            'eventTypes' => EventManagementService::EVENT_TYPES,
            'audiences' => EventManagementService::AUDIENCES,
            'statuses' => EventManagementService::STATUSES,
        ]);
    }

    public function update(Request $request, Event $event): RedirectResponse
    {
        $data = $this->validateEventRequest($request, $event);
        $data['registration_required'] = $request->boolean('registration_required');

        $this->events->update($event, $data);

        return redirect()
            ->route('extras.events.show', $event)
            ->with('success', 'Event updated.');
    }

    public function destroy(Event $event): RedirectResponse
    {
        $event->delete();

        return redirect()
            ->route('extras.events.index')
            ->with('success', 'Event removed.');
    }

    public function approve(Event $event): RedirectResponse
    {
        $this->events->approve($event, auth()->user());

        return back()->with('success', 'Event approved and published.');
    }

    public function reject(Event $event): RedirectResponse
    {
        $this->events->reject($event, auth()->user());

        return back()->with('success', 'Event rejected.');
    }
}
