export interface CopySuggestedTagData {
  tag: string
  reason?: string
  search_intent?: string
  search_type?: string
}

export interface CopySimilarAccountData {
  username: string
  reason?: string
  source?: string
  matched_source?: string[]
  confidence?: string
  status?: string
  topic_relevance?: number
  last_seen_at?: number
  profile_url?: string
  author_verified?: boolean
  follower_count?: number
  engagement_score?: number
  like_count?: number
  reply_count?: number
  post_count?: number
}

export interface CopyAudienceSampleData {
  username: string
  sample_post_id?: string
  sample_text?: string
  reply_like_count?: number
  appearances?: number
  first_seen_at: number
  last_seen_at?: number
  status?: string
}

export type AudienceSampleConfidence = 'high' | 'medium' | 'low'

export function audienceSampleConfidence(appearances?: number): AudienceSampleConfidence {
  const count = appearances ?? 0
  if (count >= 3) return 'high'
  if (count >= 2) return 'medium'
  return 'low'
}

export function audienceSampleConfidenceLabel(level: AudienceSampleConfidence): string {
  switch (level) {
    case 'high':
      return '高置信'
    case 'medium':
      return '中置信'
    default:
      return '低置信'
  }
}

export type SimilarAccountSourceTier = 'scan+web' | 'web' | 'scan' | 'unknown'

/** MatchedSource → 人類可讀標籤；後端 matched_source 陣列含哪些來源。 */
export function similarAccountSourceTier(matched?: string[]): SimilarAccountSourceTier {
  if (!matched || matched.length === 0) return 'unknown'
  const hasScan = matched.some((s) => s === 'scan')
  const hasWeb = matched.some((s) => s === 'web')
  if (hasScan && hasWeb) return 'scan+web'
  if (hasWeb) return 'web'
  if (hasScan) return 'scan'
  return 'unknown'
}

export function similarAccountSourceLabel(tier: SimilarAccountSourceTier): string {
  switch (tier) {
    case 'scan+web':
      return '海巡＋搜尋'
    case 'web':
      return '網搜'
    case 'scan':
      return '海巡'
    default:
      return '—'
  }
}

export interface CopyMissionResearchItemData {
  title?: string
  url?: string
  snippet?: string
  query?: string
}

export interface CopyMissionResearchMapData {
  audience_summary?: string
  content_goal?: string
  questions?: string[]
  pillars?: string[]
  exclusions?: string[]
  knowledge_items?: string[]
  selected_knowledge_items?: string[]
  research_items?: CopyMissionResearchItemData[]
  suggested_tags?: CopySuggestedTagData[]
  similar_accounts?: CopySimilarAccountData[]
  audience_samples?: CopyAudienceSampleData[]
  benchmark_notes?: string
}

export interface CopyMissionData {
  id: string
  persona_id: string
  label?: string
  seed_query?: string
  brief?: string
  research_map?: CopyMissionResearchMapData
  selected_tags?: string[]
  last_scan_job_id?: string
  status?: string
  create_at: number
  update_at: number
}

export function hasMissionResearchMap(mission: CopyMissionData | null | undefined): boolean {
  const map = mission?.research_map
  return !!(map?.audience_summary || (map?.suggested_tags?.length ?? 0) > 0)
}

export function copyMissionStatusLabel(status?: string): string {
  switch (status) {
    case 'mapped':
      return '地圖就緒'
    case 'scanned':
      return '已海巡'
    case 'drafted':
      return '已產稿'
    case 'archived':
      return '已封存'
    case 'open':
      return '進行中'
    default:
      return '進行中'
  }
}

export function copyMissionStatusTone(status?: string): 'brand' | 'success' | 'warning' | 'neutral' | 'sky' {
  switch (status) {
    case 'drafted':
      return 'success'
    case 'scanned':
      return 'sky'
    case 'mapped':
      return 'brand'
    default:
      return 'neutral'
  }
}
