import type { XiaobaoRiskLevel } from '../xiaobao/xiaobao-risk.types';

export const XIAOBAO_AI_PRISMA = 'XIAOBAO_AI_PRISMA';
export const XIAOBAO_AI_INTERPRETER = 'XIAOBAO_AI_INTERPRETER';
export const XIAOBAO_AI_INTERPRET_JOB = 'xiaobao.ai.interpret';
export const XIAOBAO_AI_COOLDOWN_MS = 6 * 60 * 60 * 1000;

export type XiaobaoAiQueueReason = 'queued' | 'risk_escalated';
export type XiaobaoAiSkipReason = 'policy_skip' | 'cache_hit' | 'cooldown' | 'stale';

export interface XiaobaoAiEvaluateOptions {
  now?: Date;
}

export interface XiaobaoAiInterpretPayload {
  versionId: string;
  riskSignature: string;
}

export interface XiaobaoAiRiskInterpretRequest {
  versionId: string;
  versionName: string;
  productName?: string;
  projectName?: string;
  riskScore: number;
  riskLevel: 'on_track' | 'attention' | 'at_risk' | 'likely_delayed' | 'blocked';
  expectedReleaseDate?: string | null;
  forecastReleaseDate?: string;
  delayDays: number;
  confidence: number;
  signals: {
    unfinishedCount: number;
    openBugCount: number;
    criticalBugCount: number;
    failedTestCount: number;
    blockedCount: number;
    silentRiskCount: number;
    daysToExpectedRelease?: number;
  };
  trendSummary: string;
  reasons: Array<{
    key: string;
    label: string;
    severity: 'low' | 'medium' | 'high' | 'critical';
    detail: string;
  }>;
  silentRisks: Array<{ key: string; days?: number; detail: string }>;
  dailyEvidence: {
    todayDeliveries: string[];
    todayProgress: string[];
    todayCreations: string[];
    todayRisks: string[];
    progressNotes: string[];
    needsProgressItems: string[];
    recentActivityCount: number;
    totalActivityCount: number;
    todayActualHours: number;
    lastActivityAt?: string;
  };
}

export interface XiaobaoAiRiskInsight {
  summary: string;
  why: string[];
  forecast: string;
  recommendedReleaseWindow?: string;
  suggestedActions: string[];
  ownerHints: string[];
  generatedAt: string;
}

export type XiaobaoAiRiskInterpretResponse =
  | {
    ok: true;
    result: XiaobaoAiRiskInsight;
    meta: {
      model: string;
      inputTokens: number;
      outputTokens: number;
      durationMs: number;
    };
  }
  | {
    ok: false;
    error: string;
    code: 'API_ERROR' | 'PARSE_ERROR' | 'NO_PROVIDER' | 'UNKNOWN';
  };

export interface XiaobaoAiInterpreter {
  interpretRisk(req: XiaobaoAiRiskInterpretRequest): Promise<XiaobaoAiRiskInterpretResponse>;
}

export interface XiaobaoAiEvaluateQueuedResult {
  enqueued: true;
  reason: XiaobaoAiQueueReason;
  jobId?: string;
}

export interface XiaobaoAiEvaluateSkippedResult {
  enqueued: false;
  reason: XiaobaoAiSkipReason;
}

export type XiaobaoAiEvaluateResult = XiaobaoAiEvaluateQueuedResult | XiaobaoAiEvaluateSkippedResult;

export interface XiaobaoAiGenerationResult {
  generated: boolean;
  reason: 'generated' | XiaobaoAiSkipReason;
}

export const XIAOBAO_RISK_LEVEL_RANK: Record<XiaobaoRiskLevel, number> = {
  on_track: 0,
  attention: 1,
  at_risk: 2,
  likely_delayed: 3,
  blocked: 4,
};
