<?php
declare(strict_types=1);

/** Classify external URLs (Google Suite labels now; OAuth/embed later). */
final class TicketAttachmentProvider {
    public const GENERIC = 'generic';
    public const GOOGLE_DOCS = 'google_docs';
    public const GOOGLE_SHEETS = 'google_sheets';
    public const GOOGLE_SLIDES = 'google_slides';
    public const GOOGLE_DRIVE = 'google_drive';
    public const GOOGLE_MEET = 'google_meet';
    public const GOOGLE_CHAT = 'google_chat';
    public const GOOGLE_FORMS = 'google_forms';

    /** @return array{id:string,label:string}> */
    public static function labels(): array {
        return [
            self::GENERIC => ['id' => self::GENERIC, 'label' => 'Link'],
            self::GOOGLE_DOCS => ['id' => self::GOOGLE_DOCS, 'label' => 'Google Docs'],
            self::GOOGLE_SHEETS => ['id' => self::GOOGLE_SHEETS, 'label' => 'Google Sheets'],
            self::GOOGLE_SLIDES => ['id' => self::GOOGLE_SLIDES, 'label' => 'Google Slides'],
            self::GOOGLE_DRIVE => ['id' => self::GOOGLE_DRIVE, 'label' => 'Google Drive'],
            self::GOOGLE_MEET => ['id' => self::GOOGLE_MEET, 'label' => 'Google Meet'],
            self::GOOGLE_CHAT => ['id' => self::GOOGLE_CHAT, 'label' => 'Google Chat'],
            self::GOOGLE_FORMS => ['id' => self::GOOGLE_FORMS, 'label' => 'Google Forms'],
        ];
    }

    public static function labelFor(string $provider): string {
        $labels = self::labels();
        return $labels[$provider]['label'] ?? 'Link';
    }

    public static function detectFromUrl(string $url): string {
        $host = strtolower(parse_url($url, PHP_URL_HOST) ?? '');
        if ($host === '') {
            return self::GENERIC;
        }
        if (str_contains($host, 'docs.google.com')) {
            return self::GOOGLE_DOCS;
        }
        if (str_contains($host, 'sheets.google.com')) {
            return self::GOOGLE_SHEETS;
        }
        if (str_contains($host, 'slides.google.com')) {
            return self::GOOGLE_SLIDES;
        }
        if (str_contains($host, 'drive.google.com')) {
            return self::GOOGLE_DRIVE;
        }
        if (str_contains($host, 'meet.google.com')) {
            return self::GOOGLE_MEET;
        }
        if (str_contains($host, 'chat.google.com')) {
            return self::GOOGLE_CHAT;
        }
        if (str_contains($host, 'forms.gle') || str_contains($host, 'forms.google.com')) {
            return self::GOOGLE_FORMS;
        }
        return self::GENERIC;
    }
}
