<?php

namespace App\WhatsAppPlugins;

use App\Models\WhatsAppPlugin;
use App\WhatsAppPlugins\Contracts\BaseWhatsAppPlugin;

class TelegramPlugin extends BaseWhatsAppPlugin
{
    public function getCode(): string
    {
        return 'telegram';
    }

    public function getName(): string
    {
        return 'Telegram';
    }

    public function getDescription(): string
    {
        return 'Chat via Telegram. Secure and fast messaging.';
    }

    public function getIcon(): string
    {
        return 'telegram';
    }

    public function getConfigFields(): array
    {
        return [
            [
                'key' => 'username',
                'label' => 'Telegram Username or Invite Link',
                'type' => 'text',
                'placeholder' => 'Mecaprograms',
                'required' => true,
            ],
            [
                'key' => 'url',
                'label' => 'Telegram URL',
                'type' => 'text',
                'placeholder' => 'https://t.me/Mecaprograms',
                'required' => false,
            ],
        ];
    }

    public function validateConfig(array $config): array
    {
        $validated = [];
        $validated['username'] = $config['username'] ?? '';
        $validated['url'] = $config['url'] ?? ($validated['username'] ? "https://t.me/{$validated['username']}" : '');
        return $validated;
    }

    public function isConfigured(WhatsAppPlugin $plugin): bool
    {
        $config = $plugin->config ?? [];
        return !empty($config['username']);
    }

    public function getChatUrl(WhatsAppPlugin $plugin): string
    {
        $config = $plugin->config ?? [];
        return $config['url'] ?? "https://t.me/{$config['username']}" ?? '';
    }
}
