/**
 * ASL模块 - TypeScript类型定义
 * 
 * 包含：
 * - 项目管理类型
 * - 文献管理类型
 * - 筛选结果类型
 */

// ==================== 项目管理类型 ====================

/**
 * PICOS标准
 */
export interface PICOSCriteria {
  P: string;  // 人群 Population
  I: string;  // 干预 Intervention
  C: string;  // 对照 Comparison
  O?: string; // 结局 Outcome（可选）
  S: string;  // 研究设计 Study Design
}

/**
 * 筛选配置
 */
export interface ScreeningConfig {
  style: 'lenient' | 'standard' | 'strict';  // 筛选风格
  models: string[];  // 使用的模型列表
}

/**
 * 筛选项目
 */
export interface ScreeningProject {
  id: string;
  projectName: string;
  picoCriteria: PICOSCriteria;
  inclusionCriteria: string;
  exclusionCriteria: string;
  screeningConfig: ScreeningConfig;
  status: 'draft' | 'screening' | 'completed';
  createdAt: string;
  updatedAt: string;
  userId: string;
  
  // 统计信息（可选）
  stats?: {
    totalLiteratures: number;
    screenedCount: number;
    includedCount: number;
    excludedCount: number;
    pendingCount: number;
  };
}

/**
 * 创建项目请求
 */
export interface CreateProjectRequest {
  projectName: string;
  picoCriteria: PICOSCriteria;
  inclusionCriteria: string;
  exclusionCriteria: string;
  screeningConfig: ScreeningConfig;
}

// ==================== 文献管理类型 ====================

/**
 * 文献条目
 */
export interface Literature {
  id: string;
  projectId: string;
  title: string;
  abstract: string;
  pmid?: string;
  authors?: string;
  journal?: string;
  publicationYear?: number;
  doi?: string;
  createdAt: string;
}

/**
 * 批量导入文献请求
 */
export interface ImportLiteraturesRequest {
  literatures: Omit<Literature, 'id' | 'projectId' | 'createdAt'>[];
}

// ==================== 筛选结果类型 ====================

/**
 * 判断类型（匹配后端）
 */
export type JudgmentType = 'match' | 'partial' | 'mismatch' | null;

/**
 * 结论类型
 */
export type ConclusionType = 'include' | 'exclude' | 'uncertain' | null;

/**
 * 冲突状态
 */
export type ConflictStatus = 'none' | 'conflict' | 'resolved';

/**
 * 筛选结果（完整匹配后端Schema）
 */
export interface ScreeningResult {
  id: string;
  projectId: string;
  literatureId: string;
  
  // 文献信息
  literature: Literature;
  
  // DeepSeek模型判断
  dsModelName: string;
  dsPJudgment: JudgmentType;
  dsIJudgment: JudgmentType;
  dsCJudgment: JudgmentType;
  dsSJudgment: JudgmentType;
  dsConclusion: ConclusionType;
  dsConfidence: number | null;
  
  // DeepSeek模型证据
  dsPEvidence: string | null;
  dsIEvidence: string | null;
  dsCEvidence: string | null;
  dsSEvidence: string | null;
  dsReason: string | null;
  
  // Qwen模型判断
  qwenModelName: string;
  qwenPJudgment: JudgmentType;
  qwenIJudgment: JudgmentType;
  qwenCJudgment: JudgmentType;
  qwenSJudgment: JudgmentType;
  qwenConclusion: ConclusionType;
  qwenConfidence: number | null;
  
  // Qwen模型证据
  qwenPEvidence: string | null;
  qwenIEvidence: string | null;
  qwenCEvidence: string | null;
  qwenSEvidence: string | null;
  qwenReason: string | null;
  
  // 冲突状态
  conflictStatus: ConflictStatus;
  conflictFields: string[] | null;
  
  // 最终决策
  finalDecision: 'include' | 'exclude' | 'pending' | null;
  finalDecisionBy: string | null;
  finalDecisionAt: string | null;
  exclusionReason: string | null;
  
  // AI处理状态
  aiProcessingStatus: 'pending' | 'processing' | 'completed' | 'failed';
  aiProcessedAt: string | null;
  aiErrorMessage: string | null;
  
  // 可追溯信息
  promptVersion: string;
  rawOutput: any;
  
  createdAt: string;
  updatedAt: string;
}

/**
 * 双行表格数据（用于UI展示）
 */
export interface DoubleRowData {
  key: string;
  literatureIndex: number;
  literatureId: string;
  literatureTitle: string;
  isFirstRow: boolean;
  modelName: string;
  P: JudgmentType;
  I: JudgmentType;
  C: JudgmentType;
  O: JudgmentType;
  S: JudgmentType;
  conclusion: ConclusionType;
  confidence: number | null;
  hasConflict: boolean;
  originalResult: ScreeningResult;
}

/**
 * 筛选任务（匹配后端Schema）
 */
export interface ScreeningTask {
  id: string;
  projectId: string;
  taskType: 'title_abstract' | 'full_text';
  status: 'pending' | 'running' | 'completed' | 'failed';
  
  // 进度统计
  totalItems: number;
  processedItems: number;
  successItems: number;
  failedItems: number;
  conflictItems: number;
  
  // 时间信息
  startedAt: string | null;
  completedAt: string | null;
  estimatedEndAt: string | null;
  
  // 错误信息
  errorMessage: string | null;
  
  createdAt: string;
  updatedAt: string;
}

// ==================== API响应类型 ====================

/**
 * 通用API响应
 */
export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  message?: string;
  error?: {
    code: string;
    message: string;
    details?: any;
  };
}

/**
 * 分页响应
 */
export interface PaginatedResponse<T = any> {
  items: T[];
  total: number;
  page: number;
  pageSize: number;
  hasMore: boolean;
}

// ==================== 导出统计类型 ====================

/**
 * 项目统计
 */
/**
 * 项目统计数据（Week 4 更新：匹配后端API）
 */
export interface ProjectStatistics {
  total: number;
  included: number;
  excluded: number;
  pending: number;
  conflict: number;
  reviewed: number;
  exclusionReasons: Record<string, number>;
  includedRate: string;
  excludedRate: string;
  pendingRate: string;
}

/**
 * 排除原因统计（保留用于兼容性）
 */
export interface ExclusionReasons {
  [key: string]: number;
  nonRCT: number;
  populationMismatch: number;
  interventionMismatch: number;
  comparisonMismatch: number;
  other: number;
}


