/**
 * 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'>[];
}

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

/**
 * PICO判断
 */
export interface PICOJudgment {
  P: 'match' | 'mismatch' | 'unclear';
  I: 'match' | 'mismatch' | 'unclear';
  C: 'match' | 'mismatch' | 'unclear';
  S: 'match' | 'mismatch' | 'unclear';
}

/**
 * 模型判断结果
 */
export interface ModelResult {
  modelName: string;  // 'DeepSeek-V3' | 'Qwen-Max'
  conclusion: 'include' | 'exclude';
  confidence: number;  // 0-1
  reason: string;      // 完整的判断理由
  judgment: PICOJudgment;
  evidence?: {         // 提取的证据短语
    P?: string;
    I?: string;
    C?: string;
    S?: string;
  };
}

/**
 * 筛选结果
 */
export interface ScreeningResult {
  id: string;
  projectId: string;
  literatureId: string;
  
  // 文献信息
  literature: Literature;
  
  // 双模型结果
  model1Result: ModelResult;
  model2Result: ModelResult;
  
  // 最终决策
  finalDecision: 'include' | 'exclude' | 'pending';
  decisionMethod: 'auto_agree' | 'auto_strict' | 'manual' | 'manual_batch';
  
  // 冲突信息
  hasConflict: boolean;
  conflictFields: string[];  // ['conclusion', 'P', 'I', 'C', 'S']
  
  // 人工复核
  reviewedAt?: string;
  reviewedBy?: string;
  reviewComment?: string;
  
  createdAt: string;
  updatedAt: string;
}

/**
 * 筛选任务
 */
export interface ScreeningTask {
  id: string;
  projectId: string;
  status: 'pending' | 'processing' | 'completed' | 'failed';
  totalItems: number;
  processedItems: number;
  successItems: number;
  failedItems: number;
  progress: number;  // 0-100
  startedAt?: string;
  completedAt?: string;
  errorMessage?: 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;
}

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

/**
 * 项目统计
 */
export interface ProjectStatistics {
  totalLiteratures: number;
  screenedCount: number;
  includedCount: number;
  excludedCount: number;
  pendingCount: number;
  conflictCount: number;
  reviewedCount: number;
}

/**
 * 排除原因统计
 */
export interface ExclusionReasons {
  [key: string]: number;
  nonRCT: number;
  populationMismatch: number;
  interventionMismatch: number;
  comparisonMismatch: number;
  other: number;
}

