export const SUCCESS_CODE = 102000

export interface ApiEnvelope<T> {
  code: number
  message: string
  data?: T
  error?: {
    biz_code?: string
    scope?: number
    category?: number
    detail?: number
  }
}

export interface Pagination {
  total: number
  page: number
  pageSize: number
  totalPages: number
}

export interface AuthTokenData {
  access_token: string
  refresh_token: string
  expires_in: number
  uid: string
  token_type: string
}

export interface MemberMeData {
  tenant_id: string
  uid: string
  email: string
  display_name?: string
  avatar?: string
  phone?: string
  language?: string
  currency?: string
  status: string
  origin: string
  roles?: string[]
  create_at: number
  update_at: number
}

export interface PermissionNode {
  id: string
  parent?: string
  name: string
  http_methods?: string
  http_path?: string
  status: string
  type: string
  children?: PermissionNode[]
}

export interface SettingData {
  id: string
  scope: string
  scope_id: string
  key: string
  value: Record<string, unknown>
  version: number
  create_at: number
  update_at: number
}

export interface JobData {
  id: string
  template_type: string
  template_version: number
  scope: string
  scope_id: string
  status: string
  phase: string
  worker_type: string
  payload: Record<string, unknown>
  progress: {
    summary: string
    percentage: number
    steps: Array<{
      id: string
      status: string
      message?: string
    }>
  }
  result?: Record<string, unknown>
  error?: string
  attempt: number
  max_attempts: number
  create_at: number
  update_at: number
}

export interface JobEventData {
  id: string
  job_id: string
  type: string
  from?: string
  to?: string
  message: string
  create_at: number
}

export interface JobTemplateData {
  type: string
  version: number
  name: string
  description: string
  enabled: boolean
  repeatable: boolean
  steps: Array<{
    id: string
    name: string
    worker_type: string
    timeout_seconds: number
    cancelable: boolean
  }>
}

export interface CopyResearchMapData {
  audience_summary?: string
  content_goal?: string
  questions?: string[]
  pillars?: string[]
  exclusions?: string[]
  suggested_tags?: string[]
  benchmark_notes?: string
}

export function hasCopyResearchMap(persona: PersonaData | null | undefined): boolean {
  const map = persona?.copy_research_map
  return !!(map?.audience_summary || (map?.suggested_tags?.length ?? 0) > 0)
}

export interface PersonaData {
  id: string
  display_name?: string
  persona?: string
  brief?: string
  style_profile?: string
  style_benchmark?: string
  seed_query?: string
  copy_research_map?: CopyResearchMapData
  create_at: number
  update_at: number
}

export interface ListPersonasData {
  list: PersonaData[]
}

export interface StartPersonaStyleAnalysisData {
  job_id: string
  status: string
  message?: string
}

export interface ViralScanPostData {
  id: string
  search_tag: string
  permalink: string
  author: string
  author_verified?: boolean
  follower_count?: number
  text: string
  like_count: number
  reply_count: number
  engagement_score: number
  source: string
  scan_job_id: string
  replies?: ScanReplyData[]
  create_at: number
}

export interface CopyDraftData {
  id: string
  persona_id: string
  copy_mission_id?: string
  scan_post_id?: string
  draft_type: string
  sort_order?: number
  text: string
  angle?: string
  hook?: string
  rationale?: string
  reference_notes?: string
  sources?: string[]
  status?: string
  published_media_id?: string
  published_permalink?: string
  published_at?: number
  create_at: number
}

export interface ScanReplyData {
  external_id?: string
  author?: string
  text: string
  permalink?: string
  like_count?: number
  posted_at?: string
}

export interface ThreadsAccountData {
  id: string
  display_name?: string
  username?: string
  threads_user_id?: string
  persona_id?: string
  browser_connected: boolean
  api_connected: boolean
  status: string
  create_at: number
  update_at: number
}

export interface ListThreadsAccountsData {
  list: ThreadsAccountData[]
  active_account_id: string
}

export interface ThreadsAccountConnectionPrefs {
  search_via_api: boolean
  search_source_mode: string
  publish_via_api: boolean
  dev_mode: boolean
  scrape_replies: boolean
  replies_per_post: number
  publish_headed: boolean
  playwright_debug: boolean
}

export interface ThreadsAccountConnectionData {
  account_id: string
  account_name: string
  username?: string
  browser_connected: boolean
  api_connected: boolean
  prefs: ThreadsAccountConnectionPrefs
}

export interface ImportThreadsAccountSessionData {
  success: boolean
  valid: boolean
  synced: boolean
  account_id: string
  username?: string
  message: string
  update_at: number
}

export interface ThreadsAccountAiSettingsData {
  account_id: string
  provider: string
  model: string
  research_provider?: string
  research_model?: string
  api_keys: Record<string, string>
  api_keys_configured: Record<string, boolean | string>
}

export interface JobScheduleData {
  id: string
  template_type: string
  scope: string
  scope_id: string
  enabled: boolean
  cron: string
  timezone: string
  next_run_at: number
  create_at: number
  update_at: number
}