<?php

namespace App\Services;

use App\Models\Device;
use App\Models\SystemSetting;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

/**
 * Fires `POST /api/cache-bust` on the Node bridge whenever something
 * changes that Node's per-phone settings cache would otherwise serve
 * stale for up to 5 minutes (WABA config save, branding footer change,
 * platform footer change, etc).
 *
 * Best-effort — a failed POST never blocks the user-facing save. The
 * 5-min TTL is the slow-path safety net.
 */
class NodeCacheBuster
{
    /** Flush the cache for one device's phone (or all phones in a workspace). */
    public static function bustWorkspace(int $workspaceId): void
    {
        $phones = Device::query()
            ->where('workspace_id', $workspaceId)
            ->get(['country_code', 'phone_number'])
            ->map(fn ($d) => preg_replace('/\D+/', '', (string) ($d->country_code . $d->phone_number)))
            ->filter()->values()->all();

        if (empty($phones)) {
            // Workspace has no devices yet — still flush platform-only entry.
            self::post(null);
            return;
        }
        foreach ($phones as $phone) {
            self::post($phone);
        }
    }

    /** Flush everything — used on platform-wide config changes. */
    public static function bustAll(): void
    {
        self::post(null);
    }

    private static function post(?string $phone): void
    {
        $base = (string) (SystemSetting::get('baileys_server_url', '') ?: env('SERVER_URL', ''));
        if ($base === '') return;
        $token = node_token();
        try {
            Http::withHeaders(['X-Node-Token' => $token])
                ->timeout(2)
                ->acceptJson()
                ->asJson()
                ->post(rtrim($base, '/') . '/api/cache-bust', [
                    'phone' => $phone,
                ]);
        } catch (\Throwable $e) {
            Log::info('[NodeCacheBuster] best-effort failed: ' . $e->getMessage());
        }
    }
}
