export type PlacementFlowStepKey = 'research' | 'outreach' | 'matrix'

export type WorkflowKind = 'copy' | 'placement'

export type PlacementFlowStep = {
  key: PlacementFlowStepKey
  step: string
  label: string
  shortLabel: string
  desc: string
  path: string
}

/** 流程 B（產品獲客）子步驟 — 主題列表在 /placement/topics */
export const PLACEMENT_FLOW: PlacementFlowStep[] = [
  {
    key: 'research',
    step: '01',
    label: '置入研究',
    shortLabel: '研究',
    desc: '研究地圖、知識圖譜、置入海巡',
    path: '/research',
  },
  {
    key: 'outreach',
    step: '02',
    label: '找 TA 留言',
    shortLabel: '留言',
    desc: '置入海巡結果、獲客留言草稿',
    path: '/outreach',
  },
  {
    key: 'matrix',
    step: '03',
    label: '原創矩陣',
    shortLabel: '原創',
    desc: '依獲客海巡素材產原創貼文',
    path: '/brand-matrix',
  },
]

export function detectPlacementFlowStep(pathname: string): PlacementFlowStepKey | null {
  if (pathname === '/research' || pathname.startsWith('/research/')) return 'research'
  if (pathname === '/outreach' || pathname.startsWith('/outreach/')) return 'outreach'
  if (pathname === '/brand-matrix' || pathname.startsWith('/brand-matrix/')) return 'matrix'
  if (pathname === '/placement/topics' || pathname.startsWith('/placement/')) return 'research'
  return null
}

export function detectActiveWorkflow(pathname: string): WorkflowKind | null {
  if (pathname === '/matrix' || pathname.startsWith('/matrix/')) return 'copy'
  if (detectPlacementFlowStep(pathname)) return 'placement'
  return null
}

export function isPlacementFlowPath(pathname: string): boolean {
  return detectPlacementFlowStep(pathname) !== null
}

export function brandIdFromSearch(search: string): string {
  return new URLSearchParams(search).get('brand')?.trim() ?? ''
}

export function placementFlowPath(path: string, brandId?: string): string {
  const id = brandId?.trim()
  return id ? `${path}?brand=${encodeURIComponent(id)}` : path
}