// DBIS Unified Dimensional Arbitrage Engine - Dimensional Rebalancing
// Executes rebalancing to restore dimensional equilibrium

import prisma from '@/shared/database/prisma';
import { v4 as uuidv4 } from 'uuid';
import { Decimal } from '@prisma/client/runtime/library';


export interface RebalanceResult {
  rebalanceId: string;
  arbitrageId: string;
  adjustmentAmount: Decimal;
  equilibriumRestored: boolean;
}

export class UdaeRebalanceService {
  /**
   * Execute dimensional rebalance
   * Restores dimensional equilibrium by adjusting prices across dimensions
   */
  async executeDimensionalRebalance(arbitrageId: string): Promise<RebalanceResult> {
    const rebalanceId = `UDAE-REBAL-${uuidv4()}`;

    const arbitrage = await prisma.dimensional_arbitrage.findUnique({
      where: { arbitrageId },
    });

    if (!arbitrage) {
      throw new Error(`Arbitrage calculation not found: ${arbitrageId}`);
    }

    // Calculate adjustment amount (opposite of delta to restore equilibrium)
    const adjustmentAmount = arbitrage.arbitrageDelta.negated();

    // Create rebalance record
    const rebalance = await prisma.dimensional_rebalance.create({
      data: {
        id: uuidv4(),
        rebalanceId,
        arbitrageId,
        adjustmentAmount,
        dimension: arbitrage.dimension,
        timeline: arbitrage.timeline,
        parallelBranch: arbitrage.parallelBranch,
        quantumState: arbitrage.quantumState,
        status: 'executed',
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });

    // Update arbitrage to mark as rebalanced
    await prisma.dimensional_arbitrage.update({
      where: { arbitrageId },
      data: {
        requiresRebalance: false,
        status: 'rebalanced',
        updatedAt: new Date(),
      },
    });

    // Check if equilibrium is restored (delta should be near zero after adjustment)
    const newDelta = arbitrage.arbitrageDelta.plus(adjustmentAmount);
    const equilibriumRestored = newDelta.abs().lessThan(new Decimal(0.001));

    return {
      rebalanceId,
      arbitrageId,
      adjustmentAmount,
      equilibriumRestored,
    };
  }

  /**
   * Get rebalance by ID
   */
  async getRebalance(rebalanceId: string) {
    return await prisma.dimensional_rebalance.findUnique({
      where: { rebalanceId },
      include: {
        dimensional_arbitrage: true,
      },
    });
  }

  /**
   * Get all rebalances for arbitrage
   */
  async getRebalancesForArbitrage(arbitrageId: string) {
    return await prisma.dimensional_rebalance.findMany({
      where: { arbitrageId },
      orderBy: { createdAt: 'desc' },
    });
  }

  /**
   * Get rebalances by dimension
   */
  async getRebalancesByDimension(dimension: string) {
    return await prisma.dimensional_rebalance.findMany({
      where: {
        dimension,
        status: 'executed',
      },
      orderBy: { createdAt: 'desc' },
    });
  }
}

export const udaeRebalanceService = new UdaeRebalanceService();

