/**
 * A2A Orchestration — Handoff artifact schemas and types.
 *
 * Every artifact carries a stable `runId` + `stepId` for audit tracing.
 * All artifacts are explicitly scoped to a `productId`.
 */

import { z } from 'zod';

// ── Pipeline run identity ──────────────────────────────────────────────────

export const RunContextSchema = z.object({
  runId: z.string().describe('Stable ID for the entire pipeline run'),
  stepId: z.string().describe('ID for this individual step'),
  productId: z.string().min(1),
  requestId: z.string().optional(),
  initiatedBy: z.string().optional().describe('userId who triggered the pipeline'),
});
export type RunContext = z.infer<typeof RunContextSchema>;

// ── A) SupportIncidentBrief — input to DispatcherAgent ───────────────────

export const UserReportSchema = z.object({
  summary: z.string().min(1).describe('Human-readable description of the issue'),
  platform: z.string().optional().describe('e.g. ios, android, web'),
  channel: z.string().optional().describe('e.g. keyboard_extension, dictation'),
  appVersion: z.string().optional(),
  buildNumber: z.string().optional(),
  userId: z.string().optional(),
  anonymousInstallId: z.string().optional(),
});
export type UserReport = z.infer<typeof UserReportSchema>;

export const SupportIncidentBriefSchema = z.object({
  productId: z.string().min(1),
  userReport: UserReportSchema,
  timeWindow: z
    .object({
      from: z.string().datetime().optional(),
      to: z.string().datetime().optional(),
    })
    .default({}),
  openDiagnosticsSession: z
    .boolean()
    .default(false)
    .describe('If true and userId/anonymousInstallId present, open a live diagnostics session'),
});
export type SupportIncidentBrief = z.infer<typeof SupportIncidentBriefSchema>;

// ── B) DispatchDecision — output of DispatcherAgent ───────────────────────

export interface DispatchDecision {
  runContext: RunContext;
  brief: SupportIncidentBrief;
  steps: ('telemetry_analyst' | 'diagnostics_orchestrator' | 'report_writer')[];
  /** Resolved time window (fills defaults if omitted in brief) */
  resolvedTimeWindow: { from: string; to: string };
}

// ── C) TelemetryFindings — output of TelemetryAnalystAgent ───────────────

export interface ClusterRef {
  clusterId: string;
  pk: string;
  fingerprint: string;
  severity: string;
  totalCount: number;
  lastSeenAt: string;
  platform: string;
  module: string;
  sampleMessage?: string;
}

export interface TelemetryFindings {
  runContext: RunContext;
  clusters: ClusterRef[];
  topHypotheses: string[];
  recommendedActions: string[];
  queryError?: string;
}

// ── D) DiagnosticsSessionResult — output of DiagnosticsOrchestratorAgent ─

export interface DiagnosticsSessionResult {
  runContext: RunContext;
  skipped: boolean;
  skipReason?: string;
  session?: {
    id: string;
    status: string;
    expiresAt: string;
    collectionLevel: string;
    targetUserId?: string;
    targetAnonymousId?: string;
  };
  sessionError?: string;
}

// ── E) FinalIncidentReport — output of ReportWriterAgent ─────────────────

export interface FinalIncidentReport {
  runId: string;
  productId: string;
  generatedAt: string;
  summary: string;
  clusterCount: number;
  clusters: ClusterRef[];
  topHypotheses: string[];
  recommendedActions: string[];
  diagnosticsSession: DiagnosticsSessionResult['session'] | null;
  markdownReport: string;
}
