<?php

namespace App\Support;

use App\Models\AdminSite;

final class AdminIntegrationSitePresenter
{
    /**
     * @return array<string, mixed>
     */
    public static function listItem(AdminSite $site, bool $hasLineRoot = false): array
    {
        return [
            'id' => (int) $site->id,
            'code' => (string) $site->code,
            'name' => (string) $site->name,
            'has_line_root' => $hasLineRoot,
            'currency_code' => (string) $site->currency_code,
            'status' => (int) $site->status,
            'wallet_api_url' => $site->wallet_api_url,
            'lottery_h5_base_url' => $site->lottery_h5_base_url,
            'wallet_timeout_seconds' => (int) ($site->wallet_timeout_seconds ?? 10),
            'has_sso_secret' => is_string($site->sso_jwt_secret_encrypted) && $site->sso_jwt_secret_encrypted !== '',
            'has_wallet_api_key' => is_string($site->wallet_api_key_encrypted) && $site->wallet_api_key_encrypted !== '',
            'sso_secret_masked' => is_string($site->sso_jwt_secret_encrypted) && $site->sso_jwt_secret_encrypted !== ''
                ? '••••••••'
                : null,
            'wallet_api_key_masked' => is_string($site->wallet_api_key_encrypted) && $site->wallet_api_key_encrypted !== ''
                ? '••••••••'
                : null,
            'is_default' => (bool) $site->is_default,
            'updated_at' => $site->updated_at?->toIso8601String(),
        ];
    }

    /**
     * @return array<string, mixed>
     */
    public static function detail(AdminSite $site): array
    {
        return [
            ...self::listItem($site),
            'wallet_debit_path' => (string) $site->wallet_debit_path,
            'wallet_credit_path' => (string) $site->wallet_credit_path,
            'wallet_balance_path' => (string) $site->wallet_balance_path,
            'iframe_allowed_origins' => $site->iframe_allowed_origins ?? [],
            'lottery_h5_base_url' => $site->lottery_h5_base_url,
            'notes' => $site->notes,
            'created_at' => $site->created_at?->toIso8601String(),
        ];
    }

    /**
     * @param  array{sso_jwt_secret: string, wallet_api_key: string}  $secrets
     * @return array<string, mixed>
     */
    public static function withPlainSecretsOnce(array $payload, array $secrets): array
    {
        return array_merge($payload, [
            'secrets' => [
                'sso_jwt_secret' => $secrets['sso_jwt_secret'],
                'wallet_api_key' => $secrets['wallet_api_key'],
            ],
            'secrets_display_once' => true,
        ]);
    }

    /**
     * 交接/联调参数表（不含密钥明文）。
     *
     * @return array<string, mixed>
     */
    public static function parameterSheet(AdminSite $site): array
    {
        $walletBase = is_string($site->wallet_api_url) && $site->wallet_api_url !== ''
            ? rtrim($site->wallet_api_url, '/')
            : null;

        $fullUrl = static function (?string $path) use ($walletBase): ?string {
            if ($walletBase === null || ! is_string($path) || $path === '') {
                return null;
            }

            return $walletBase.'/'.ltrim($path, '/');
        };

        $h5Base = $site->lottery_h5_base_url;
        if (! is_string($h5Base) || trim($h5Base) === '') {
            $h5Base = config('lottery.main_site.base_url');
        }

        return [
            'exported_at' => now()->toIso8601String(),
            'site_code' => (string) $site->code,
            'name' => (string) $site->name,
            'status' => (int) $site->status === 1 ? 'enabled' : 'disabled',
            'currency_code' => (string) $site->currency_code,
            'lottery_h5_base_url' => $h5Base,
            'wallet_api_url' => $site->wallet_api_url,
            'wallet_balance_url' => $fullUrl($site->wallet_balance_path),
            'wallet_debit_url' => $fullUrl($site->wallet_debit_path),
            'wallet_credit_url' => $fullUrl($site->wallet_credit_path),
            'wallet_balance_path' => (string) $site->wallet_balance_path,
            'wallet_debit_path' => (string) $site->wallet_debit_path,
            'wallet_credit_path' => (string) $site->wallet_credit_path,
            'wallet_timeout_seconds' => (int) ($site->wallet_timeout_seconds ?? 10),
            'iframe_allowed_origins' => $site->iframe_allowed_origins ?? [],
            'sso_secret_configured' => is_string($site->sso_jwt_secret_encrypted) && $site->sso_jwt_secret_encrypted !== '',
            'sso_secret_masked' => is_string($site->sso_jwt_secret_encrypted) && $site->sso_jwt_secret_encrypted !== ''
                ? '••••••••'
                : null,
            'wallet_api_key_configured' => is_string($site->wallet_api_key_encrypted) && $site->wallet_api_key_encrypted !== '',
            'wallet_api_key_masked' => is_string($site->wallet_api_key_encrypted) && $site->wallet_api_key_encrypted !== ''
                ? '••••••••'
                : null,
            'notes' => $site->notes,
            'security_note' => '密钥明文仅于创建/重置时展示一次，请勿通过导出或邮件长期传播。',
        ];
    }
}
