/**
 * Pluggable AI agent interfaces for Multideal admin automation.
 *
 * Concrete implementations are injected at runtime via the registry.
 * Phase 2 ships stub implementations; real models are swapped in future phases.
 */

// ─── ImageApprovalAgent ───────────────────────────────────────────────────────

export interface SearchEngineImageReviewInput {
  imageUrl: string;
  dealTitle: string;
  category: string;
}

export interface SearchEngineImageReviewResult {
  decision: 'approve' | 'reject';
  confidence: number;
  reason?: string;
}

export interface UploadedImageReviewInput {
  imageUrl: string;
  dealTitle: string;
  uploaderType: 'user' | 'vendor';
}

export interface UploadedImageReviewResult {
  decision: 'auto_approve' | 'needs_review';
  confidence: number;
  flags?: string[];
}

/**
 * AI agent responsible for image approval decisions.
 *
 * Two flows:
 * - Search engine images: LLM checks relevance + content policy → auto-approve if OK.
 * - Vendor/user uploads: content screening → queued for human review unless clearly safe.
 */
export interface ImageApprovalAgent {
  reviewSearchEngineImage(
    input: SearchEngineImageReviewInput,
  ): Promise<SearchEngineImageReviewResult>;

  reviewUploadedImage(input: UploadedImageReviewInput): Promise<UploadedImageReviewResult>;
}

// ─── ReviewRemovalPrescoringAgent ─────────────────────────────────────────────

export interface ReviewRemovalPrescoringInput {
  reviewBody: string;
  reviewRating: number | null;
  vendorReply?: string;
  removalReason: string;
}

export interface ReviewRemovalPrescoringResult {
  shouldRemove: boolean;
  confidence: number;
  explanation: string;
}

/**
 * AI agent that pre-scores vendor review-removal requests.
 *
 * Provides a recommendation to the human admin based on the review content,
 * vendor reply, and stated removal reason.
 */
export interface ReviewRemovalPrescoringAgent {
  score(input: ReviewRemovalPrescoringInput): Promise<ReviewRemovalPrescoringResult>;
}

// ─── VendorViolationFlagAgent ─────────────────────────────────────────────────

export interface VendorViolationSignals {
  unresolvedComplaints: number;
  avgRating: number;
  removedReviews: number;
  refundRate: number;
  daysSinceFirstSale: number;
}

export interface VendorViolationFlagInput {
  vendorId: string;
  signals: VendorViolationSignals;
}

export interface VendorViolationFlagResult {
  severity: 'none' | 'low' | 'medium' | 'high';
  reasons: string[];
}

/**
 * AI agent that evaluates vendor health signals and assigns a violation severity.
 *
 * Used as a background signal to surface potentially problematic vendors
 * to the human admin before they cause customer harm.
 */
export interface VendorViolationFlagAgent {
  flag(input: VendorViolationFlagInput): Promise<VendorViolationFlagResult>;
}

// ─── SupportAgent ─────────────────────────────────────────────────────────────

export interface SupportAgentRunInput {
  parentType: 'ticket' | 'case';
  parentId: string;
  trigger: 'opened' | 'escalated' | 'user_reply' | 'evidence_uploaded' | 'vendor_timeout';
}

export interface SupportAgentRunResult {
  decision: 'auto' | 'escalate' | 'ask' | 'propose';
  confidence: number;
  toolCallsMade: number;
  totalCostUsd: number;
  escalated: boolean;
}

export interface SupportAgent {
  run(input: SupportAgentRunInput): Promise<SupportAgentRunResult>;
}

// ─── VendorAdmissionReviewAgent ───────────────────────────────────────────────

export interface VendorAdmissionInput {
  vendorId: string;
  businessName: string;
  ownerEmail: string;
  /** ISO 8601 phone country prefix extracted at registration time (e.g. "+972"). */
  phoneCountryPrefix: string;
  createdAt: Date;
}

export interface VendorAdmissionResult {
  decision: 'APPROVE' | 'FLAG' | 'REJECT';
  rationale: string;
}

/**
 * AI agent that performs an admission review when a vendor row is first created.
 *
 * Called when llmDecision IS NULL (first review). Returns an admission decision
 * with rationale. The same concrete class also handles post-approval violation
 * flagging via the `flag()` method (triggered when llmDecision IS NOT NULL).
 */
export interface VendorAdmissionReviewAgent {
  review(input: VendorAdmissionInput): Promise<VendorAdmissionResult>;
}
