// Settlement Arbitration Service
// Arbitration tribunal logic

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


export interface ArbitrationDecisionRequest {
  disputeId: string;
  tribunalDecision: string;
  decisionType: string; // final, binding, appealable
}

export interface ArbitrationResult {
  arbitrationId: string;
  decisionType: string;
  status: string;
}

export class SettlementArbitrationService {
  /**
   * Create arbitration decision
   */
  async createDecision(
    request: ArbitrationDecisionRequest
  ): Promise<ArbitrationResult> {
    const dispute = await prisma.settlementDispute.findUnique({
      where: { disputeId: request.disputeId },
    });

    if (!dispute) {
      throw new Error(`Dispute not found: ${request.disputeId}`);
    }

    if (dispute.stage !== 'arbitration_tribunal') {
      throw new Error(`Dispute must be in arbitration stage: ${dispute.stage}`);
    }

    const arbitrationId = `ARB-${uuidv4()}`;

    const arbitration = await prisma.settlementArbitration.create({
      data: {
        arbitrationId,
        disputeId: request.disputeId,
        tribunalDecision: request.tribunalDecision,
        decisionType: request.decisionType,
        status: 'decided',
        decidedAt: new Date(),
      },
    });

    // Update dispute status
    await prisma.settlementDispute.update({
      where: { disputeId: request.disputeId },
      data: {
        status: 'resolved',
        resolution: request.tribunalDecision,
        resolvedAt: new Date(),
      },
    });

    return {
      arbitrationId: arbitration.arbitrationId,
      decisionType: arbitration.decisionType,
      status: arbitration.status,
    };
  }

  /**
   * Enforce arbitration decision
   */
  async enforceDecision(arbitrationId: string): Promise<void> {
    const arbitration = await prisma.settlementArbitration.findUnique({
      where: { arbitrationId },
    });

    if (!arbitration) {
      throw new Error(`Arbitration not found: ${arbitrationId}`);
    }

    if (arbitration.status !== 'decided') {
      throw new Error(`Arbitration must be decided: ${arbitration.status}`);
    }

    await prisma.settlementArbitration.update({
      where: { arbitrationId },
      data: {
        status: 'enforced',
        enforcedAt: new Date(),
      },
    });
  }

  /**
   * Get arbitration decision
   */
  async getDecision(disputeId: string): Promise<any | null> {
    const arbitration = await prisma.settlementArbitration.findFirst({
      where: { disputeId },
      include: { dispute: true },
      orderBy: { decidedAt: 'desc' },
    });

    return arbitration;
  }

  /**
   * Get all arbitration decisions
   */
  async getAllDecisions(): Promise<any[]> {
    const arbitrations = await prisma.settlementArbitration.findMany({
      include: { dispute: true },
      orderBy: { decidedAt: 'desc' },
    });

    return arbitrations;
  }
}

export const settlementArbitrationService = new SettlementArbitrationService();

