<?php

namespace App\Services\Integration;

/**
 * 运行时主站接入配置（由库表或 legacy env 解析而来）。
 */
final readonly class PartnerSiteConfig
{
    public const SOURCE_DATABASE = 'database';

    public const SOURCE_LEGACY_ENV = 'legacy_env';

    public function __construct(
        public string $siteCode,
        public bool $enabled,
        public ?string $walletApiUrl,
        public string $walletDebitPath,
        public string $walletCreditPath,
        public string $walletBalancePath,
        public string $walletLookupIdempotentPath,
        public ?string $ssoJwtSecret,
        public ?string $walletApiKey,
        public int $walletTimeoutSeconds,
        public string $source,
    ) {}

    public function hasWalletApi(): bool
    {
        return is_string($this->walletApiUrl) && trim($this->walletApiUrl) !== '';
    }

    public function hasSsoSecret(): bool
    {
        return is_string($this->ssoJwtSecret) && $this->ssoJwtSecret !== '';
    }

    /**
     * 可安全写入 Cache 的数组形态（避免 readonly 对象序列化产生 __PHP_Incomplete_Class）。
     *
     * @return array<string, mixed>
     */
    public function toCacheArray(): array
    {
        return [
            'site_code' => $this->siteCode,
            'enabled' => $this->enabled,
            'wallet_api_url' => $this->walletApiUrl,
            'wallet_debit_path' => $this->walletDebitPath,
            'wallet_credit_path' => $this->walletCreditPath,
            'wallet_balance_path' => $this->walletBalancePath,
            'wallet_lookup_idempotent_path' => $this->walletLookupIdempotentPath,
            'sso_jwt_secret' => $this->ssoJwtSecret,
            'wallet_api_key' => $this->walletApiKey,
            'wallet_timeout_seconds' => $this->walletTimeoutSeconds,
            'source' => $this->source,
        ];
    }

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromCacheArray(array $data): self
    {
        return new self(
            siteCode: (string) ($data['site_code'] ?? ''),
            enabled: (bool) ($data['enabled'] ?? false),
            walletApiUrl: isset($data['wallet_api_url']) && is_string($data['wallet_api_url']) && $data['wallet_api_url'] !== ''
                ? $data['wallet_api_url']
                : null,
            walletDebitPath: (string) ($data['wallet_debit_path'] ?? '/wallet/debit-for-lottery'),
            walletCreditPath: (string) ($data['wallet_credit_path'] ?? '/wallet/credit-from-lottery'),
            walletBalancePath: (string) ($data['wallet_balance_path'] ?? '/wallet/balance'),
            walletLookupIdempotentPath: (string) ($data['wallet_lookup_idempotent_path'] ?? '/wallet/lookup-idempotent'),
            ssoJwtSecret: isset($data['sso_jwt_secret']) && is_string($data['sso_jwt_secret']) && $data['sso_jwt_secret'] !== ''
                ? $data['sso_jwt_secret']
                : null,
            walletApiKey: isset($data['wallet_api_key']) && is_string($data['wallet_api_key']) && $data['wallet_api_key'] !== ''
                ? $data['wallet_api_key']
                : null,
            walletTimeoutSeconds: max(1, (int) ($data['wallet_timeout_seconds'] ?? 10)),
            source: (string) ($data['source'] ?? self::SOURCE_DATABASE),
        );
    }
}
