/** 文献作者 */
export interface Author {
  family?: string
  given?: string
  affiliation?: string
  [key: string]: unknown
}

/** 系统标签 */
export interface LiteratureTag {
  id: number | string
  category?: string
  name_zh?: string
  path?: string
  is_major?: boolean
  [key: string]: unknown
}

/** 研究设计分类 */
export interface StudyDesign {
  primary?: string
  sub?: string
  design?: string | null
  label_zh?: string
}

/** 临床试验注册 */
export interface TrialReg {
  nct?: string
  eudract?: string
  chictr?: string
}

/** 基金资助 */
export interface Grant {
  agency?: string
  grant_id?: string
  [key: string]: unknown
}

/** PRISMA 流程数据 */
export interface PrismaFlowData {
  identification_total: number
  duplicates_removed: number
  screened: number
  excluded_screening: number
  full_text_assessed: number
  excluded_eligibility: number
  included_total: number
  excluded_reasons: Record<string, number>
}

/** 方法学小结 */
export interface MethodsSummary {
  summary: string
  language: string
}

/** 许可信息 */
export interface LicenseInfo {
  href?: string
  type?: string
  text?: string
}

/** RCT 检测结果 */
export interface RctDetection {
  is_rct?: boolean
  confidence?: 'confirmed' | 'suspected'
  source?: 'pub_type' | 'keyword'
  evidence?: string
}

/** PICO 数据结构 */
export interface PicoData {
  population?: string | null
  intervention?: string | null
  comparator?: string | null
  outcome?: string | null
  sample_size?: number | null
  effect_size?: {
    hr?: number | null
    or?: number | null
    rr?: number | null
    ci_lower?: number | null
    ci_upper?: number | null
    p_value?: number | null
    [key: string]: unknown
  } | null
}

/** 效应量解析结果 */
export interface ParsedEffect {
  endpoint: string
  effect_type: string | null
  value: number | null
  ci_lower: number | null
  ci_upper: number | null
  p_value: number | null
  raw: string
}

/** Outcome 表数据 */
export interface OutcomeTable {
  caption: string
  headers: string[]
  rows?: string[][]
  table_type: string
  _note: string
  parsed_effects?: ParsedEffect[]
}

/** 基线特征数据 */
export interface BaselineData {
  table1_caption?: string | null
  arms?: string[]
  extracted_fields?: Record<string, Record<string, string>>
  parsed_values?: Record<string, Record<string, unknown>>
  header_sample_sizes?: Record<string, { n: number; raw: string }>
}

/** 提取数据响应 */
export interface ExtractedDataResponse {
  pmid: number
  title?: string
  pico?: PicoData | null
  baseline?: BaselineData | null
  outcome_tables?: OutcomeTable[]
  tables?: {
    caption: string
    headers: string[]
    rows: string[][]
  }[]
  parsed_effects?: ParsedEffect[]
  sections?: { heading?: string; text: string }[]
  _note: string
}

/** 预提取数据（Table 1 基线） */
export interface PreExtractedData {
  table1_caption?: string
  arms?: string[]
  extracted_fields?: Record<string, Record<string, string>>
  ai_extracted?: Record<string, unknown>
}

/** PubMed 补充解析——化合物 */
export interface ChemicalSubstance {
  name?: string
  registry_number?: string
  mesh_ui?: string
}

/** PubMed 补充解析——数据库引用 */
export interface DataBankEntry {
  databank_name?: string
  accession_numbers?: string[]
}

/** PubMed 补充解析——补充 MeSH */
export interface SupplMeshEntry {
  descriptor?: string
  ui?: string
  type?: string
}

/** 文献对象（列表用） */
export interface LiteratureItem {
  pmid: number
  title?: string
  doi?: string
  first_author?: string
  affiliation?: string
  journal?: string
  journal_tier?: string | number
  pub_date?: string
  article_date?: string | null
  pub_year?: number | string
  volume?: string
  issue?: string
  pages?: string
  pub_types?: string[]
  authors?: Author[]
  is_oa?: boolean
  pmc_id?: string
  cited_by_count?: number
  tags?: LiteratureTag[]
  keywords?: string[]
  grants?: Grant[]
  abstract?: string
  ai_summary?: string
  study_design?: StudyDesign | null
  trial_reg?: TrialReg | null
  retracted?: boolean
  is_negative_result?: boolean
  rct_detection?: RctDetection | null
  created_at?: string
  updated_at?: string
  [key: string]: unknown
}

/** 文献详情（带所有字段） */
export interface LiteratureDetail extends LiteratureItem {
  language?: string
  source?: string
  pico?: Record<string, unknown> | null
  mesh_headings?: { descriptor?: string; ui?: string; major?: boolean; qualifiers?: string[] }[]
  full_text_sections?: { tables?: any[]; sections?: any[] } | null
  retraction_details?: { ref_source?: string; ref_pmid?: number } | null
  license_info?: LicenseInfo | null
  pre_extracted_data?: PreExtractedData | null
  chemical_list?: ChemicalSubstance[]
  gene_symbols?: string[]
  num_refs?: number | null
  publication_status?: string | null
  article_date?: string | null
  databank_list?: DataBankEntry[]
  suppl_mesh_list?: SupplMeshEntry[]
  reading_status?: string | null
}

/** 系统评价项目 */
export interface SystematicReview {
  id: string
  title: string
  objective?: string
  protocol?: string
  prisma_stage: string
  status: string
  inclusion_criteria: string[]
  exclusion_criteria: string[]
  databases_searched: string[]
  search_strategy: Record<string, unknown>
  notes?: string
  created_at?: string
  updated_at?: string
}

/** 评价文献 */
export interface ReviewLiterature {
  id: string
  review_id: string
  literature_id: string
  pmid: number
  title?: string
  first_author?: string
  journal?: string
  pub_date?: string
  doi?: string
  stage: string
  exclusion_reason?: string
  notes?: string
  quality_score?: string
  study_design?: StudyDesign | null
  retracted?: boolean
  pico?: Record<string, unknown> | null
  created_at?: string
}

/** 笔记 */
export interface NoteItem {
  id: string
  literature_id?: string
  pmid?: number | null
  literature_title?: string
  _show?: boolean
  author_name?: string | null
  content: string
  quote_text?: string | null
  is_private?: boolean
  created_at?: string
  updated_at?: string
  [key: string]: unknown
}

/** 个人标签 */
export interface PersonalTag {
  name: string
  [key: string]: unknown
}

/** 通知项 */
export interface NotificationItem {
  id: string
  is_read: boolean
  title: string
  content?: string
  created_at: string
  [key: string]: unknown
}

/** 收藏状态 */
export interface SavedItem {
  pmid: number
  rating?: number
  personal_tags?: PersonalTag[]
  reading_status?: string
  [key: string]: unknown
}

/** Feed 推送项（带优先级） */
export interface FeedItem extends LiteratureItem {
  id: string
  priority?: string
}

/** 标签选项（筛选面板使用） */
export interface TagOption {
  id: string | number
  name_zh: string
  level: number
  parent_id?: string | null
  is_selectable?: boolean
  tag_category?: string
  [key: string]: unknown
}

/** 搜索请求体 */
export interface SearchRequestBody {
  query: string
  field: string
  page: number
  page_size: number
  sort?: string
  year_from?: number
  year_to?: number
  date_from?: string
  date_to?: string
  journal_tiers?: string[]
  tag_ids?: string[]
}

/** 个人文件夹 */
export interface FolderItem {
  id: string
  name: string
  description?: string | null
  color: string
  sort_order: number
  article_count: number
  created_at?: string
}

/** 通用分页响应 */
export interface PaginatedResponse<T> {
  items: T[]
  total: number
  total_pages?: number
  page?: number
  page_size?: number
}
