// ILC Dimension Service
// Dimension definitions (D0: Classical, D1: DLT, D2: Quantum, D3: Simulated, D4: Parallel)

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


export interface CreateDimensionRequest {
  dimensionCode: 'D0' | 'D1' | 'D2' | 'D3' | 'D4';
  dimensionName: string;
  dimensionMetadata: Record<string, unknown>;
}

export class IlcDimensionService {
  /**
   * Create ledger dimension
   */
  async createDimension(ledgerId: string, request: CreateDimensionRequest) {
    const dimensionId = `ILC-DIM-${uuidv4()}`;

    const dimension = await prisma.ledger_dimensions.create({
      data: {
        dimensionId,
        ledgerId,
        dimensionCode: request.dimensionCode,
        dimensionName: request.dimensionName,
        dimensionMetadata: request.dimensionMetadata,
        status: 'active',
      },
    });

    return dimension;
  }

  /**
   * Get dimension by ID
   */
  async getDimension(dimensionId: string) {
    const dimension = await prisma.ledger_dimensions.findUnique({
      where: { dimensionId },
      include: {
        ledger: true,
      },
    });

    if (!dimension) {
      throw new Error(`Dimension not found: ${dimensionId}`);
    }

    return dimension;
  }

  /**
   * Get dimensions for ledger
   */
  async getDimensionsForLedger(ledgerId: string) {
    return prisma.ledger_dimensions.findMany({
      where: { ledgerId },
      orderBy: {
        dimensionCode: 'asc',
      },
    });
  }

  /**
   * Initialize standard dimensions for a ledger
   */
  async initializeStandardDimensions(ledgerId: string) {
    const dimensions = [
      {
        dimensionCode: 'D0' as const,
        dimensionName: 'D0 - Classical Finance',
        dimensionMetadata: {
          type: 'classical',
          description: 'Traditional & CBDC systems',
        },
      },
      {
        dimensionCode: 'D1' as const,
        dimensionName: 'D1 - Distributed Ledger Systems',
        dimensionMetadata: {
          type: 'dlt',
          description: 'Blockchain + DLT environments',
        },
      },
      {
        dimensionCode: 'D2' as const,
        dimensionName: 'D2 - Quantum State Finance',
        dimensionMetadata: {
          type: 'quantum',
          description: 'GQL and entanglement-linked state commitments',
        },
      },
      {
        dimensionCode: 'D3' as const,
        dimensionName: 'D3 - Simulated Economic Realms',
        dimensionMetadata: {
          type: 'simulated',
          description: 'AI-generated economic universes',
        },
      },
      {
        dimensionCode: 'D4' as const,
        dimensionName: 'D4 - Parallel Monetary States (Theory)',
        dimensionMetadata: {
          type: 'parallel',
          description: 'Potential cross-dimensional consistency rules',
        },
      },
    ];

    const created = [];
    for (const dim of dimensions) {
      const createdDim = await this.createDimension(ledgerId, dim);
      created.push(createdDim);
    }

    return created;
  }
}

export const ilcDimensionService = new IlcDimensionService();

