// Node Type Manager Service
// Manage GPU edge node types: MGN, SAN, ZKN, QGN
// Supplement B: MetaverseDubai Integration Framework

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


export interface CreateComputeNodeRequest {
  nodeType: 'MGN' | 'SAN' | 'ZKN' | 'QGN';
  regionId: string;
  metaverseNodeId?: string;
  latency: number; // milliseconds
  gpuCapacity: number; // GPU units
  networkAddress: string;
  sixGEnabled?: boolean;
  zkVerificationEnabled?: boolean;
  holographicRenderingEnabled?: boolean;
  metadata?: Record<string, unknown>;
}

export interface ComputeNodeResult {
  nodeId: string;
  nodeType: string;
  status: string;
}

export class NodeTypeManagerService {
  /**
   * Create compute node
   * Node types:
   * - MGN: Metaverse GPU Node (for metaverse rendering)
   * - SAN: Sovereign AI Node (for AI operations)
   * - ZKN: Zero-Knowledge Node (for ZK verification)
   * - QGN: Quantum Gateway Node (for quantum operations)
   */
  async createComputeNode(
    request: CreateComputeNodeRequest
  ): Promise<ComputeNodeResult> {
    logger.info('Node Type Manager: Creating compute node', { request });

    const nodeId = `COMPUTE-${uuidv4()}`;

    const computeNode = await prisma.metaverseComputeNode.create({
      data: {
        nodeId,
        nodeType: request.nodeType,
        regionId: request.regionId,
        metaverseNodeId: request.metaverseNodeId || null,
        latency: request.latency,
        gpuCapacity: request.gpuCapacity,
        networkAddress: request.networkAddress,
        sixGEnabled: request.sixGEnabled ?? false,
        zkVerificationEnabled: request.zkVerificationEnabled ?? false,
        holographicRenderingEnabled:
          request.holographicRenderingEnabled ?? false,
        status: 'active',
        metadata: request.metadata || {},
      },
    });

    logger.info('Node Type Manager: Compute node created', { nodeId });

    return {
      nodeId: computeNode.nodeId,
      nodeType: computeNode.nodeType,
      status: computeNode.status,
    };
  }

  /**
   * Get compute nodes by type
   */
  async getComputeNodesByType(nodeType: 'MGN' | 'SAN' | 'ZKN' | 'QGN') {
    return await prisma.metaverseComputeNode.findMany({
      where: {
        nodeType,
        status: 'active',
      },
      orderBy: { latency: 'asc' },
    });
  }

  /**
   * Get compute nodes by region
   */
  async getComputeNodesByRegion(regionId: string) {
    return await prisma.metaverseComputeNode.findMany({
      where: {
        regionId,
        status: 'active',
      },
      orderBy: { latency: 'asc' },
    });
  }

  /**
   * Update compute node status
   */
  async updateComputeNodeStatus(
    nodeId: string,
    status: 'active' | 'degraded' | 'offline'
  ): Promise<void> {
    await prisma.metaverseComputeNode.update({
      where: { nodeId },
      data: { status },
    });

    logger.info('Node Type Manager: Compute node status updated', {
      nodeId,
      status,
    });
  }
}

export const nodeTypeManagerService = new NodeTypeManagerService();

