import {
  aiBudgetsRecordSpend,
  reviewsCreate,
  supportCasesCreate,
} from '../../lib/platform-client.js';
import type { PlatformClientOptions } from '../../lib/platform-client.js';

interface BudgetSpendInput {
  productId: string;
  runId: string;
  source: string;
  scopeType?: 'product' | 'agent';
  scopeId?: string;
  agentId?: string;
  agentVersionId?: string;
  evaluationRunId?: string;
  model?: string;
  tokensUsed?: number;
  costUsd: number;
}

interface SupportCaseInput {
  productId: string;
  runId: string;
  title: string;
  description: string;
  priority?: 'critical' | 'high' | 'medium' | 'low';
  source?: 'manual' | 'agent' | 'telemetry' | 'customer';
  requesterUserId?: string;
  tags?: string[];
}

interface ReviewInput {
  productId: string;
  orgId: string;
  workspaceId?: string;
  runId: string;
  title: string;
  description: string;
  category: string;
  priority?: 'low' | 'normal' | 'high' | 'urgent';
  actionType: string;
  metadata?: Record<string, unknown>;
}

export async function recordBudgetSpend(
  input: BudgetSpendInput,
  opts: PlatformClientOptions
): Promise<void> {
  if (input.costUsd <= 0) return;
  await safeGovernanceCall(() =>
    aiBudgetsRecordSpend(
      {
        scopeType: input.scopeType ?? 'product',
        scopeId: input.scopeId ?? input.productId,
        agentId: input.agentId,
        agentVersionId: input.agentVersionId,
        runId: input.runId,
        evaluationRunId: input.evaluationRunId,
        model: input.model,
        tokensUsed: input.tokensUsed ?? 0,
        costUsd: input.costUsd,
        source: input.source,
      },
      withProduct(opts, input.productId)
    )
  );
}

export async function createSupportCaseForRun(
  input: SupportCaseInput,
  opts: PlatformClientOptions
): Promise<void> {
  await safeGovernanceCall(() =>
    supportCasesCreate(
      {
        requesterUserId: input.requesterUserId,
        title: input.title,
        description: input.description,
        priority: input.priority ?? 'high',
        source: input.source ?? 'agent',
        runId: input.runId,
        tags: input.tags ?? [],
      },
      withProduct(opts, input.productId)
    )
  );
}

export async function createReviewForRun(
  input: ReviewInput,
  opts: PlatformClientOptions
): Promise<void> {
  await safeGovernanceCall(() =>
    reviewsCreate(
      {
        title: input.title,
        description: input.description,
        category: input.category,
        priority: input.priority ?? 'high',
        scope: input.workspaceId ? 'workspace' : 'org',
        orgId: input.orgId,
        workspaceId: input.workspaceId,
        runId: input.runId,
        source: 'mcp.a2a',
        actionType: input.actionType,
        metadata: input.metadata,
      },
      withProduct(opts, input.productId)
    )
  );
}

function withProduct(opts: PlatformClientOptions, productId: string): PlatformClientOptions {
  return {
    token: opts.token,
    requestId: opts.requestId,
    productId,
  };
}

async function safeGovernanceCall(fn: () => Promise<unknown>): Promise<void> {
  try {
    await fn();
  } catch {
    // Governance hooks must not fail the pipeline itself.
  }
}
