/**
 * AI Agent Client Interface
 *
 * Anti-corruption layer abstraction for all external AI agent cluster communication.
 * Application services depend on this interface, never on concrete HTTP implementations.
 */

export const AI_AGENT_CLIENT = Symbol('IAiAgentClient');

// ── Chat ──────────────────────────────────────────────────────────────────────

export interface AgentChatRequest {
  userId: string;
  message: string;
  sessionId?: string;
  context?: Record<string, any>;
}

export interface AgentChatResponse {
  reply: string;
  sessionId: string;
  suggestions: string[];
}

// ── Credit Scoring ────────────────────────────────────────────────────────────

export interface AgentCreditScoreRequest {
  userId: string;
  issuerId?: string;
  redemptionRate: number;
  breakageRate: number;
  tenureDays: number;
  satisfactionScore: number;
}

export interface AgentCreditScoreResponse {
  score: number;
  level: string;
  factors: Record<string, number>;
  recommendations?: string[];
}

// ── Pricing ───────────────────────────────────────────────────────────────────

export interface AgentPricingRequest {
  couponId: string;
  faceValue: number;
  daysToExpiry: number;
  totalDays: number;
  redemptionRate: number;
  liquidityPremium: number;
}

export interface AgentPricingResponse {
  suggestedPrice: number;
  confidence: number;
  factors: Record<string, number>;
}

// ── Anomaly Detection ─────────────────────────────────────────────────────────

export interface AgentAnomalyRequest {
  userId: string;
  transactionType: string;
  amount: number;
  metadata?: Record<string, any>;
}

export interface AgentAnomalyResponse {
  isAnomalous: boolean;
  riskScore: number;
  reasons: string[];
}

// ── Admin ─────────────────────────────────────────────────────────────────────

export interface AgentStatsResponse {
  sessionsToday: number;
  totalSessions: number;
  avgResponseTimeMs: number;
  satisfactionScore: number;
  activeModules: number;
}

export interface AgentTopQuestion {
  question: string;
  count: number;
  category: string;
}

export interface AgentSessionSummary {
  sessionId: string;
  userId: string;
  messageCount: number;
  startedAt: string;
  lastMessageAt: string;
  satisfactionRating: number | null;
}

export interface AgentSessionsResponse {
  items: AgentSessionSummary[];
  total: number;
  page: number;
  limit: number;
}

export interface AgentSatisfactionMetrics {
  averageRating: number;
  totalRatings: number;
  distribution: Record<string, number>;
  trend: { period: string; rating: number }[];
}

// ── Interface ─────────────────────────────────────────────────────────────────

export interface IAiAgentClient {
  /** Send a chat message to the AI agent. */
  chat(req: AgentChatRequest): Promise<AgentChatResponse>;

  /** Request a credit score from the AI agent. */
  creditScore(req: AgentCreditScoreRequest): Promise<AgentCreditScoreResponse>;

  /** Request a pricing suggestion from the AI agent. */
  pricing(req: AgentPricingRequest): Promise<AgentPricingResponse>;

  /** Request anomaly detection from the AI agent. */
  anomalyDetect(req: AgentAnomalyRequest): Promise<AgentAnomalyResponse>;

  /** Get aggregate agent stats (admin). */
  getStats(): Promise<AgentStatsResponse>;

  /** Get top questions (admin). */
  getTopQuestions(limit: number): Promise<AgentTopQuestion[]>;

  /** Get sessions (admin). */
  getSessions(page: number, limit: number): Promise<AgentSessionsResponse>;

  /** Get satisfaction metrics (admin). */
  getSatisfactionMetrics(): Promise<AgentSatisfactionMetrics>;

  /** Propagate module config to external agent. */
  configureModule(moduleId: string, config: Record<string, any>): Promise<void>;

  /** Check external agent health. */
  healthCheck(): Promise<boolean>;
}
