<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Services\SupportCenter;

use App\Exceptions\SupportCenter\SupportCenterException;
use Illuminate\Support\Facades\Log;

/**
 * 站点在线状态心跳（site_no 缺失时跳过，失败不影响页面主流程）。
 */
final class HostSiteOnboardingHeartbeatService
{
    public function __construct(
        private readonly SupportCenterOpenApiTransport $transport,
        private readonly SupportCenterRequestContextBuilder $contextBuilder,
        private readonly HostSiteOnboardingStatusCacheStore $statusCacheStore,
        private readonly SupportCenterAuditLogger $auditLogger,
    ) {}

    public function send(string $message = 'ok'): bool
    {
        $siteNo = $this->resolveSiteNo();

        if ($siteNo === null) {
            Log::info('support_center.site_presence.skipped', [
                'reason' => 'site_no_missing',
            ]);

            return false;
        }

        $payload = array_filter(
            $this->contextBuilder->sitePresenceBody($message, $siteNo),
            static fn (mixed $value): bool => $value !== null && $value !== '',
        );

        try {
            $this->transport->post(
                SupportCenterOpenHostEndpoints::SITE_PRESENCE,
                $payload,
                'site_presence',
            );

            Log::info('support_center.site_presence.sent', [
                'site_no' => $siteNo,
            ]);

            $this->auditLogger->logSitePresenceReported($siteNo);

            return true;
        } catch (SupportCenterException $exception) {
            Log::warning('support_center.site_presence.failed', [
                'site_no' => $siteNo,
                'error_code' => $exception->errorCode(),
                'request_id' => $exception->requestId(),
            ]);

            $this->auditLogger->logSitePresenceFailed($siteNo, $exception);

            return false;
        } catch (\Throwable $exception) {
            Log::warning('support_center.site_presence.failed', [
                'site_no' => $siteNo,
                'exception_class' => $exception::class,
            ]);

            return false;
        }
    }

    private function resolveSiteNo(): ?string
    {
        $configured = config('dberp.support_center.site_no');

        if (is_string($configured) && trim($configured) !== '') {
            return trim($configured);
        }

        $cached = $this->statusCacheStore->get();
        $siteNo = $cached['site_no'] ?? null;

        if (is_string($siteNo) && trim($siteNo) !== '') {
            return trim($siteNo);
        }

        return null;
    }
}
