<?php

namespace App\WhatsAppPlugins;

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

class MessengerPlugin extends BaseWhatsAppPlugin
{
    public function getCode(): string
    {
        return 'messenger';
    }

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

    public function getDescription(): string
    {
        return 'Chat via Facebook Messenger.';
    }

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

    public function getConfigFields(): array
    {
        return [
            [
                'key' => 'page_id',
                'label' => 'Facebook Page ID or Username',
                'type' => 'text',
                'placeholder' => 'mecaprograms1',
                'required' => true,
            ],
            [
                'key' => 'url',
                'label' => 'Messenger URL',
                'type' => 'text',
                'placeholder' => 'https://m.me/mecaprograms1',
                'required' => false,
            ],
        ];
    }

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

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

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