<?php

namespace App\Filament\Tenant\Pages;

use App\Tenancy\TenantManager;
use Filament\Notifications\Notification;
use Filament\Pages\Page;

class Integrations extends Page
{
    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-puzzle-piece';

    protected static ?string $navigationLabel = 'Integrări';

    protected static string|\UnitEnum|null $navigationGroup = 'Marketing';

    protected static ?int $navigationSort = 63;

    protected static ?string $title = 'Integrări externe';

    protected string $view = 'filament.tenant.pages.integrations';

    public array $config = [];

    public function mount(): void
    {
        $tenant = app(TenantManager::class)->current();
        $this->config = (array) ($tenant?->settings['integrations'] ?? []);
    }

    public function setStatus(string $key, bool $enabled): void
    {
        $this->config[$key]['enabled'] = $enabled;
        $this->save();
    }

    public function updateField(string $key, string $field, ?string $value): void
    {
        $this->config[$key][$field] = $value;
    }

    public function save(): void
    {
        $tenant = app(TenantManager::class)->current();
        if (! $tenant) return;
        $tenant->update([
            'settings' => array_merge((array) $tenant->settings, [
                'integrations' => $this->config,
            ]),
        ]);
        Notification::make()->title('Integrări salvate')->success()->send();
    }

    public function integrations(): array
    {
        return [
            'telegram_bot' => [
                'name' => 'Telegram Bot',
                'icon' => '✈️',
                'color' => '#229ED9',
                'description' => 'Primește cereri din canalul Telegram + trimite confirmări automate.',
                'fields' => [
                    'bot_token' => ['label' => 'Bot Token', 'type' => 'password', 'placeholder' => 'din @BotFather'],
                    'chat_id' => ['label' => 'Channel/Chat ID', 'type' => 'text', 'placeholder' => '@psauto_md'],
                ],
            ],
            'whatsapp_business' => [
                'name' => 'WhatsApp Business',
                'icon' => '💬',
                'color' => '#25D366',
                'description' => 'Trimite mesaje template către clienți (programări, confirmări).',
                'fields' => [
                    'phone_id' => ['label' => 'Phone Number ID', 'type' => 'text'],
                    'token' => ['label' => 'Permanent Access Token', 'type' => 'password'],
                ],
            ],
            'google_ads' => [
                'name' => 'Google Ads',
                'icon' => '🔍',
                'color' => '#EA4335',
                'description' => 'Tracking conversii — leadurile generate de campanii Google.',
                'fields' => [
                    'customer_id' => ['label' => 'Customer ID', 'type' => 'text', 'placeholder' => '123-456-7890'],
                    'conversion_id' => ['label' => 'Conversion ID', 'type' => 'text'],
                ],
            ],
            'facebook' => [
                'name' => 'Facebook / Instagram Lead Ads',
                'icon' => '📘',
                'color' => '#1877F2',
                'description' => 'Importă lead-uri direct din Lead Ads forms.',
                'fields' => [
                    'page_id' => ['label' => 'Page ID', 'type' => 'text'],
                    'access_token' => ['label' => 'Access Token', 'type' => 'password'],
                ],
            ],
            'sms_gateway' => [
                'name' => 'SMS Gateway',
                'icon' => '📱',
                'color' => '#6366F1',
                'description' => 'Trimite SMS-uri prin gateway local (Moldcell/Orange API).',
                'fields' => [
                    'provider' => ['label' => 'Provider', 'type' => 'text', 'placeholder' => 'moldcell / orange / twilio'],
                    'api_key' => ['label' => 'API Key', 'type' => 'password'],
                    'sender_id' => ['label' => 'Sender ID', 'type' => 'text'],
                ],
            ],
            'webhook_in' => [
                'name' => 'Webhook intrări (formulare site)',
                'icon' => '🪝',
                'color' => '#8B5CF6',
                'description' => 'URL secret pentru POST direct cu lead-uri din site-ul tău.',
                'fields' => [
                    'secret' => ['label' => 'Secret token', 'type' => 'text', 'placeholder' => 'random string'],
                ],
            ],
        ];
    }
}
