import path from 'node:path'

import type { RuntimeConfig } from '../types/runtime-config.js'

export function createDefaultRuntimeConfig(projectRoot: string): RuntimeConfig {
  return {
    server: {
      port: 3000,
      // 默认信任一层反代（Caddy），req.ip 才能取到真实客户端地址；直连暴露端口时设为 false。
      trustProxy: 1,
    },

    data: {
      root: path.resolve(projectRoot, 'data'),
    },

    logging: {
      level: 'info',
      // 对接日志默认只保留 warn/error，避免 integration-*.log 被 info 刷爆
      integrationLevel: 'warn',
      retentionDays: 30,
      maxFileSizeMb: 20,
      maxFiles: 5,
    },

    retention: {
      // 原始报文只保留追溯所需窗口，清理后保留订单/事件摘要。
      rawPayloadDays: 180,
      taskEventDays: 180,
      webhookEventDays: 180,
      auditLogDays: 365,
    },

    database: {
      url: 'postgres://postgres:postgres@127.0.0.1:5432/order_site',
      ssl: false,
      maxConnections: 8,
      idleTimeoutMs: 30_000,
      connectionTimeoutMs: 5_000,
      statementTimeoutMs: 15_000,
      slowQueryThresholdMs: 500,
    },

    storage: {
      mode: 'minio',
      endpoint: 'http://127.0.0.1:9000',
      bucket: 'order-site',
      accessKeyId: 'minioadmin',
      secretAccessKey: 'minioadmin',
      region: 'us-east-1',
      maxUploadSizeMb: 10,
      oss: {
        endpoint: '',
        bucket: '',
        accessKeyId: '',
        secretAccessKey: '',
        region: 'oss-cn-hangzhou',
      },
    },

    orders: {
      claimBaseUrl: 'http://127.0.0.1:5173/#/claim',
      // 0 表示长期有效；后台手动关闭任务后领取链接仍会立即失效。
      tokenTtlHours: 0,
    },

    admin: {
      sessionSecret: '',
      sessionTtlHours: 12,
      defaultUsers: [],
    },

    platforms: {
      ninetyone: {
        userId: '',
        secret: '',
        version: '1.0',
        shopId: '91kaquan',
        shopName: '91卡券',
        timestampToleranceSeconds: 600,
        cardsEncoding: 'aes-256-ecb-base64',
      },
      kuaishouIndustry: {
        appKey: '',
        appSecret: '',
        signSecret: '',
        messageSecret: '',
        provider: 'kuaishou-industry',
        platform: 'kuaishou',
        shopId: 'kuaishou-industry',
        shopName: '快手行业电子凭证',
        version: '1',
      },
      kuaishouFeifei: {
        enabled: true,
        baseUrl: 'http://skin-exchange.yiquyou.icu',
        appKey: '',
        appSecret: '',
        timeoutMs: 10000,
        notifyUrl: '',
        productRules: [],
      },
      affiliateDash: {
        enabled: true,
        baseUrl: '',
        appKey: '',
        appSecret: '',
        callbackSecret: '',
        timeoutMs: 10000,
        notifyUrl: '',
        timestampToleranceSeconds: 300,
        preferredMatchEnabled: true,
        skuMapping: {},
      },
      cloudtentacles: {
        baseUrl: 'https://123.207.217.176',
        timeoutMs: 5000,
        sendSmsPath: '/public/verif_code',
        loginPath: '/public/login',
        userInfoPath: '/user/info',
        assetPath: '/user/get_asset',
        permissionPath: '/user/get_permission',
        categoriesPath: '/categories/get',
        skuListPath: '/sku/list',
        skuBuyPath: '/sku/buy',
        skuUsePath: '/sku/use',
        knapsackPath: '/user/get_knapsack',
        vnListPath: '/vn/list',
        vnAppointPath: '/vn/appoint',
        vnGenerateLoginCodePath: '/vn/generate_login_code',
        vnVerifCodePath: '/public/vn_verif_code',
        vnVerifyLoginCodePath: '/vn/verify_login_code',
        vnBindUrlPath: '/vn/bind_url',
        vnBindInfoPath: '/vn/bind_info',
        vnBackPath: '/vn/back',
        bindUrlTtlSeconds: 600,
        bindUrlProbeIntervalSeconds: 30,
        bindUrlProbeTimeoutMs: 5000,
        bindUrlProbeUserAgent:
          'Mozilla/5.0 (Linux; Android 12; M2012K11AC Build/SKQ1.211006.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/98.0.4758.102 MQQBrowser/6.2 TBS/046141 Mobile Safari/537.36 V1_AND_SQ_8.8.85_2674_YYB_D QQ/8.8.85.7685 NetType/WIFI WebP/0.3.0 Pixel/1080 StatusBarHeight/108 SimpleUISwitch/0 QQTheme/1000',
        bindUrlProbeEndpoint: 'https://comm.ams.game.qq.com/ide/',
        bindUrlProbeChartId: '323794',
        bindUrlProbeSubChartId: '323794',
        bindUrlProbeIdeToken: 'z90Syo',
        bindUrlProbeActivityUrl: 'http%3A%2F%2Fgp.qq.com%2Fcp%2Fa20240828cmcc%2F',
        bindUrlProbeReferer: 'https://gp.qq.com/',
        bindUrlProbeExtraCookie: '',
        clientSource: 'ct-client',
        deviceId: '08bc9d8c-fd15-48ea-bc00-8d754076cafc',
        deviceType: 1,
        publicKeyPem: `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsSzjk+9st2f8IpYqYaM5
F7K27ZR2IvzxTr2w4zgZAS2+BRdgMvXUOITdFQT+ytYF/r3pIgTkL7MF9UQuylmb
S57DiqGBLNBS7FzKyYzWTOc9yYA3+KrarbvpUn/Bm3UK+KKB17viPy+0pnNAHysJ
pzqadmAtQhI1ZDujJ2sW+FkV/pC8KIFahiyPIHjncy1ZG/BbRF3BDhvDcr6O7scO
GsHstGf9XsZBIITS5LYmlai7OdY4ZIVxUtQbfWzibihO+YDJJzFgt47drX0Ajo85
PfQTPNA++KsXtwRX9M6Re3vkTDRsutIIWKtj8jqhUbbYS3vzS8GJnAWavUFVkR15
0wIDAQAB
-----END PUBLIC KEY-----`,
      },
    },

    cors: {
      allowedOrigins: ['*'],
    },

    worker: {
      sms: {
        provider: 'mock',
        accessKeyId: '',
        accessKeySecret: '',
        signName: '',
        templateCode: '',
        codeTtlSeconds: 300,
        cooldownSeconds: 60,
        dailyLimit: 10,
      },
      orderTimelineVisible: false,
    },
  }
}
