<?php

namespace App\Support;

use App\Models\Room;

class EventsSourceLink
{
    public static function isLinked(Room $room): bool
    {
        $source = (array) ($room->source ?? []);

        return ($source['app'] ?? '') === 'events' && (int) ($source['event_id'] ?? 0) > 0;
    }

    public static function eventId(Room $room): ?int
    {
        if (! self::isLinked($room)) {
            return null;
        }

        return (int) ((array) ($room->source ?? []))['event_id'];
    }

    public static function baseUrl(): string
    {
        return rtrim((string) config('meet.events_app_url', 'https://events.ladill.com'), '/');
    }

    public static function isApiConfigured(): bool
    {
        return filled(config('meet.events_api_url')) && filled(config('meet.events_api_key'));
    }

    public static function createEventUrl(?Room $room = null): string
    {
        return self::baseUrl().'/events/create';
    }

    public static function eventAdminUrl(Room $room): ?string
    {
        $eventId = self::eventId($room);

        return $eventId ? self::baseUrl().'/events/'.$eventId.'/attendees' : null;
    }

    public static function eventEditUrl(Room $room): ?string
    {
        $eventId = self::eventId($room);

        return $eventId ? self::baseUrl().'/events/'.$eventId : null;
    }

    public static function meetReturnUrl(Room $room): string
    {
        return route('meet.join', $room, absolute: true);
    }

    public static function eventRegistrationUrl(Room $room, ?string $meetReturn = null): ?string
    {
        $source = (array) ($room->source ?? []);
        $stored = trim((string) ($source['registration_url'] ?? ''));

        $url = $stored !== ''
            ? $stored
            : (($eventId = self::eventId($room))
                ? self::baseUrl().'/q/'.($source['short_code'] ?? '')
                : null);

        if ($url === null) {
            return null;
        }

        $meetReturn = trim((string) ($meetReturn ?? ''));
        if ($meetReturn === '') {
            return $url;
        }

        return $url.(str_contains($url, '?') ? '&' : '?').'meet_return='.urlencode($meetReturn);
    }

    public static function eventQrUrl(Room $room): ?string
    {
        $eventId = self::eventId($room);

        return $eventId ? self::baseUrl().'/events/'.$eventId.'/download/png' : null;
    }

    public static function eventCommsUrl(Room $room): ?string
    {
        $eventId = self::eventId($room);

        return $eventId ? self::baseUrl().'/events/'.$eventId.'/attendees#comms' : null;
    }

    public static function eventBadgesUrl(Room $room): ?string
    {
        $eventId = self::eventId($room);

        return $eventId ? self::baseUrl().'/events/'.$eventId.'/badges' : null;
    }

    /** @return array<string, string|null> */
    public static function urls(Room $room): array
    {
        return [
            'admin' => self::eventAdminUrl($room),
            'edit' => self::eventEditUrl($room),
            'registration' => self::eventRegistrationUrl($room),
            'qr' => self::eventQrUrl($room),
            'comms' => self::eventCommsUrl($room),
            'badges' => self::eventBadgesUrl($room),
        ];
    }

    public static function eventTitle(Room $room): ?string
    {
        if (! self::isLinked($room)) {
            return null;
        }

        $title = trim((string) (((array) ($room->source ?? []))['event_title'] ?? ''));

        return $title !== '' ? $title : null;
    }
}
