<?php

namespace App\Listeners\LiveChat;

use App\Events\Conversations\HumanRequestedEvent;
use App\Models\Agent;
use App\Models\Conversation;
use App\Models\Lead;
use App\Models\Workspace;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

/**
 * Pings external chat tools (Slack, Microsoft Teams) when a visitor
 * asks for a human. Listener fires on the queue so the visitor's
 * request-human HTTP response stays fast — webhooks aren't on the
 * critical path.
 *
 * Webhook payloads are deliberately compact:
 *   - Slack: a single `text` field with the URL inline (Slack
 *     auto-unfurls the link).
 *   - Teams: a MessageCard with a "View conversation" button.
 *
 * Both formats accept the standard incoming-webhook URL — buyers paste
 * the URL Slack/Teams gives them, no extra OAuth dance.
 */
class LiveChatNotifier implements ShouldQueue
{
    public function handle(HumanRequestedEvent $event): void
    {
        $conversation = Conversation::query()->withoutGlobalScopes()->find($event->conversationId);
        if ($conversation === null) {
            return;
        }
        $agent = Agent::query()->withoutGlobalScopes()->find($conversation->agent_id);
        if ($agent === null) {
            return;
        }
        $workspace = Workspace::query()->withoutGlobalScopes()->find($agent->workspace_id);
        if ($workspace === null) {
            return;
        }

        $slack = (string) ($workspace->slack_webhook_url ?? '');
        $teams = (string) ($workspace->teams_webhook_url ?? '');

        if ($slack === '' && $teams === '') {
            return;
        }

        $lead = Lead::query()->withoutGlobalScopes()
            ->where('conversation_id', $conversation->id)
            ->latest()
            ->first();

        $convUrl = url("/app/conversations/{$conversation->id}");
        $emailLine = $lead?->email ? "Visitor email: {$lead->email}" : 'No email captured yet.';
        $pageLine = $conversation->page_url
            ? "Page: {$conversation->page_url}"
            : 'No page URL.';
        $title = "🔔 New live-chat request — {$agent->name}";

        if ($slack !== '') {
            $this->postSlack($slack, $title, $convUrl, $emailLine, $pageLine);
        }

        if ($teams !== '') {
            $this->postTeams($teams, $title, $convUrl, $emailLine, $pageLine, $agent->name);
        }
    }

    private function postSlack(
        string $url,
        string $title,
        string $convUrl,
        string $emailLine,
        string $pageLine,
    ): void {
        $text = "{$title}\n{$emailLine}\n{$pageLine}\n<{$convUrl}|Open conversation>";

        try {
            Http::timeout(5)->post($url, ['text' => $text]);
        } catch (\Throwable $e) {
            // Swallowed — webhook outages mustn't break the request flow.
            Log::warning('LiveChat slack webhook failed', ['error' => $e->getMessage()]);
        }
    }

    private function postTeams(
        string $url,
        string $title,
        string $convUrl,
        string $emailLine,
        string $pageLine,
        string $agentName,
    ): void {
        $payload = [
            '@type' => 'MessageCard',
            '@context' => 'https://schema.org/extensions',
            'summary' => $title,
            'themeColor' => 'F59E0B',
            'title' => $title,
            'sections' => [[
                'facts' => [
                    ['name' => 'Agent', 'value' => $agentName],
                    ['name' => 'Email', 'value' => $emailLine],
                    ['name' => 'Page', 'value' => $pageLine],
                ],
            ]],
            'potentialAction' => [[
                '@type' => 'OpenUri',
                'name' => 'Open conversation',
                'targets' => [['os' => 'default', 'uri' => $convUrl]],
            ]],
        ];

        try {
            Http::timeout(5)->post($url, $payload);
        } catch (\Throwable $e) {
            Log::warning('LiveChat teams webhook failed', ['error' => $e->getMessage()]);
        }
    }
}
