// Gap Audit Engine Service
// Main gap scanning across multiverse systems, temporal ledgers, quantum chains, cognitive-intent layers, DLT/metaverse ecosystems

import prisma from '@/shared/database/prisma';
import { v4 as uuidv4 } from 'uuid';
import { logger } from '@/infrastructure/monitoring/logger';
import { gapDetectionService } from './gap-detection.service';
import { moduleGeneratorService } from './module-generator.service';
import { recommendationsEngineService } from './recommendations-engine.service';


export interface GapAuditRequest {
  auditScope: Array<
    'multiverse' | 'temporal' | 'quantum' | 'cognitive' | 'dlt' | 'metaverse'
  >;
  includeRecommendations?: boolean;
}

export interface GapAuditResult {
  auditId: string;
  gapsFound: number;
  modulesGenerated: number;
  recommendationsCount: number;
  status: string;
}

export class GapAuditEngineService {
  /**
   * Execute comprehensive gap audit
   * Scans all DBIS systems for gaps
   */
  async executeGapAudit(
    request: GapAuditRequest
  ): Promise<GapAuditResult> {
    logger.info('Gap Audit: Starting comprehensive audit', { request });

    const auditId = `GAP-AUDIT-${uuidv4()}`;

    // Step 1: Create audit record
    const audit = await prisma.gapAudit.create({
      data: {
        auditId,
        auditScope: request.auditScope as any,
        status: 'running',
      },
    });

    try {
      // Step 2: Scan for gaps in each scope
      const allGaps: any[] = [];

      for (const scope of request.auditScope) {
        const gaps = await gapDetectionService.detectGaps(scope);
        allGaps.push(...gaps);

        // Save gap detections
        for (const gap of gaps) {
          await prisma.gapDetection.create({
            data: {
              detectionId: `GAP-DET-${uuidv4()}`,
              auditId,
              gapType: gap.gapType,
              systemScope: scope,
              description: gap.description,
              severity: gap.severity,
              status: 'detected',
            },
          });
        }
      }

      // Step 3: Generate modules for detected gaps
      let modulesGenerated = 0;
      for (const gap of allGaps) {
        if (gap.autoGenerate) {
          const moduleId = await moduleGeneratorService.generateModule(
            gap.gapType
          );
          if (moduleId) {
            modulesGenerated++;
          }
        }
      }

      // Step 4: Generate recommendations if requested
      let recommendationsCount = 0;
      if (request.includeRecommendations) {
        const recommendations =
          await recommendationsEngineService.generateRecommendations(allGaps);
        recommendationsCount = recommendations.length;

        for (const recommendation of recommendations) {
          await prisma.systemRecommendation.create({
            data: {
              recommendationId: `REC-${uuidv4()}`,
              auditId,
              recommendationType: recommendation.type,
              title: recommendation.title,
              description: recommendation.description,
              priority: recommendation.priority,
              status: 'pending',
            },
          });
        }
      }

      // Step 5: Update audit status
      await prisma.gapAudit.update({
        where: { auditId },
        data: {
          status: 'completed',
          gapsFound: allGaps.length,
          modulesGenerated,
          recommendationsCount,
          completedAt: new Date(),
        },
      });

      logger.info('Gap Audit: Audit completed', {
        auditId,
        gapsFound: allGaps.length,
        modulesGenerated,
      });

      return {
        auditId,
        gapsFound: allGaps.length,
        modulesGenerated,
        recommendationsCount,
        status: 'completed',
      };
    } catch (error) {
      await prisma.gapAudit.update({
        where: { auditId },
        data: {
          status: 'failed',
        },
      });

      throw error;
    }
  }

  /**
   * Get audit by ID
   */
  async getAudit(auditId: string) {
    return await prisma.gapAudit.findUnique({
      where: { auditId },
      include: {
        detections: true,
        recommendations: true,
      },
    });
  }

  /**
   * Get audit history
   */
  async getAuditHistory(limit: number = 100) {
    return await prisma.gapAudit.findMany({
      orderBy: { createdAt: 'desc' },
      take: limit,
      include: {
        detections: true,
        recommendations: true,
      },
    });
  }
}

export const gapAuditEngineService = new GapAuditEngineService();

