// Threat Classification Service
// T1 (Technical), T2 (Financial), T3 (Coordination)

import prisma from '@/shared/database/prisma';
import { v4 as uuidv4 } from 'uuid';


export class ThreatClassificationService {
  /**
   * Classify threat
   */
  async classifyThreat(
    threatType: string,
    severity: string,
    affectedBanks: string[]
  ): Promise<string> {
    // Classify as T1, T2, or T3
    let category = 'T1'; // Default to Technical

    if (threatType.includes('fx') || threatType.includes('liquidity') || threatType.includes('bank_run')) {
      category = 'T2'; // Financial
    } else if (threatType.includes('coordinated') || threatType.includes('multi_scb') || threatType.includes('insider')) {
      category = 'T3'; // Coordination
    }

    return category;
  }

  /**
   * Create supra-sovereign threat
   */
  async createThreat(
    threatCategory: string,
    threatType: string,
    severity: string,
    affectedBanks: string[],
    description: string
  ) {
    const threatId = `THREAT-${uuidv4()}`;

    return await prisma.supra_sovereign_threats.create({
      data: {
        threatId,
        threatCategory,
        threatType,
        severity,
        affectedBanks,
        coordinationLevel: affectedBanks.length > 1 ? 'multi_scb' : 'single_scb',
        description,
        status: 'detected',
      },
    });
  }

  /**
   * Get threats by category
   */
  async getThreatsByCategory(category: string) {
    return await prisma.supra_sovereign_threats.findMany({
      where: {
        threatCategory: category,
      },
      orderBy: {
        detectedAt: 'desc',
      },
    });
  }
}

export const threatClassificationService = new ThreatClassificationService();

