import db from '../config/database';
import { AuditService } from './AuditService';
import { SovereigntyIdentityService } from './SovereigntyIdentityService';
import { FeatureGovernanceService } from '../core/governance/FeatureGovernanceService';

/**
 * [BIZ_FIN_20] 基于主权身份的全球授信池 (Sovereign Credit Pool)
 * 负责根据租户的主权身份 (DID) 与聚合声誉，自动分配并动态调整全球范围内的授信额度
 */
export class SovereignCreditPoolService {
  /**
   * 初始化租户授信池
   */
  static async initializeCreditPool(tenantId: string, traceId: string): Promise<void> {
    // Feature Flag Check
    if (!(await FeatureGovernanceService.isEnabled('BIZ_FIN_CREDIT_POOL', tenantId))) {
      return;
    }

    const identity = await SovereigntyIdentityService.getIdentity(tenantId);
    if (!identity) throw new Error('Sovereignty Identity not found');

    // 1. 基于声誉分计算初始乘数 (模拟)
    const reputation = JSON.parse(identity.reputation_score);
    const multiplier = reputation.fulfillment > 90 ? 1.5 : 1.0;
    const initialLimit = 50000 * multiplier;

    await db.transaction(async (trx) => {
      await trx('cf_sov_credit_pool').insert({
        tenant_id: tenantId,
        max_credit_limit: initialLimit,
        available_credit: initialLimit,
        reputation_multiplier: multiplier,
        status: 'ACTIVE'
      });

 // 4. 审计日志
    await AuditService.log({
      tenantId: tenantId,
      userId: 'SYSTEM',
      module: 'CREDIT_POOL',
      action: 'ESTABLISH_CREDIT_PROFILE',
      resourceType: 'FINANCE_CREDIT',
      resourceId: tenantId,
      traceId: traceId,
      afterSnapshot: JSON.stringify({ initialLimit, multiplier }),
      result: 'success',
      source: 'node',
      metadata: { did: identity.did }
    });
    });
  }

  /**
   * 动态调整授信额度 (BIZ_FIN_20)
   */
  static async refreshCreditLimit(tenantId: string, traceId: string): Promise<void> {
    const pool = await db('cf_sov_credit_pool').where({ tenant_id: tenantId }).first();
    if (!pool) return;

    // 1. 获取最新跨平台声誉
    await SovereigntyIdentityService.syncCrossPlatformReputation(tenantId, traceId);
    const identity = await SovereigntyIdentityService.getIdentity(tenantId);
    const reputation = JSON.parse(identity.reputation_score);

    // 2. 重新计算乘数
    const newMultiplier = reputation.aggregated_market_score > 4.8 ? 2.0 : 1.2;
    const newLimit = 50000 * newMultiplier;

    await db('cf_sov_credit_pool')
      .where({ tenant_id: tenantId })
      .update({
        max_credit_limit: newLimit,
        reputation_multiplier: newMultiplier,
        updated_at: db.fn.now()
      });

    await AuditService.log({
      tenantId: tenantId,
      userId: 'SYSTEM',
      module: 'CREDIT_POOL',
      action: 'SOV_CREDIT_LIMIT_REFRESHED',
      resourceType: 'FINANCE_CREDIT',
      resourceId: tenantId,
      traceId: traceId,
      afterSnapshot: JSON.stringify({ newLimit, newMultiplier }),
      result: 'success',
      source: 'node',
      metadata: { score: reputation.aggregated_market_score }
    });
  }

  /**
   * 使用授信 (预占)
   */
  static async useCredit(tenantId: string, amount: number, traceId: string): Promise<boolean> {
    return await db.transaction(async (trx) => {
      const pool = await trx('cf_sov_credit_pool')
        .where({ tenant_id: tenantId })
        .forUpdate()
        .first();

      if (!pool || pool.available_credit < amount) return false;

      await trx('cf_sov_credit_pool')
        .where({ tenant_id: tenantId })
        .decrement('available_credit', amount);

      return true;
    });
  }
}
