import type { ThreadsAccountConnectionData } from '../types/api'
import { navGroups } from './acAssets'
import { filterAdminOnlyNavGroups, type NavGroup } from './memberRole'

export type OnboardingStep = 'account' | 'connection' | 'persona'

export type OnboardingSnapshot = {
  loading: boolean
  hasAccounts: boolean
  connectionReady: boolean
  hasPersonas: boolean
  isComplete: boolean
  nextStep: OnboardingStep | null
}

export const onboardingNavGroups: NavGroup[] = [
  {
    label: '入門設定',
    items: [
      { to: '/settings', label: '設定', icon: 'settings' },
      { to: '/personas', label: '人設', icon: 'persona', matchPrefix: '/personas' },
    ],
  },
]

export function connectionIsReady(connection: ThreadsAccountConnectionData | null) {
  if (!connection) return false
  const devMode = !!connection.prefs?.dev_mode
  return devMode ? connection.browser_connected : connection.api_connected
}

export function buildOnboardingSnapshot(input: {
  accountsLoading: boolean
  personasLoading: boolean
  connectionLoading: boolean
  accountCount: number
  personaCount: number
  connectionReady: boolean
}): OnboardingSnapshot {
  const loading = input.accountsLoading || input.personasLoading || input.connectionLoading
  const hasAccounts = input.accountCount > 0
  const hasPersonas = input.personaCount > 0
  const connectionReady = hasAccounts && input.connectionReady
  const isComplete = hasAccounts && connectionReady && hasPersonas

  let nextStep: OnboardingStep | null = null
  if (!loading && !isComplete) {
    if (!hasAccounts) nextStep = 'account'
    else if (!connectionReady) nextStep = 'connection'
    else if (!hasPersonas) nextStep = 'persona'
  }

  return {
    loading,
    hasAccounts,
    connectionReady,
    hasPersonas,
    isComplete,
    nextStep,
  }
}

export function navGroupsForOnboarding(complete: boolean, isAdmin = false): NavGroup[] {
  const groups: NavGroup[] = complete ? navGroups : onboardingNavGroups
  return filterAdminOnlyNavGroups(groups, isAdmin)
}

export function isOnboardingAllowedPath(pathname: string) {
  if (pathname === '/') return true
  if (pathname === '/settings') return true
  if (pathname === '/personas' || pathname.startsWith('/personas/')) return true
  if (pathname === '/brands' || pathname.startsWith('/brands/')) return true
  if (pathname === '/placement/topics' || pathname.startsWith('/placement/topics/')) return true
  if (pathname === '/research' || pathname.startsWith('/research/')) return true
  if (pathname === '/outreach' || pathname.startsWith('/outreach/')) return true
  if (pathname === '/matrix' || pathname.startsWith('/matrix/')) return true
  if (pathname.startsWith('/threads/')) return true
  return false
}

export function onboardingRedirectPath(pathname: string, complete: boolean) {
  if (complete || isOnboardingAllowedPath(pathname)) return null
  return '/settings'
}

export function onboardingNavApps(complete: boolean, isAdmin = false) {
  return navGroupsForOnboarding(complete, isAdmin).flatMap((group) => group.items)
}

export function onboardingGlowClass(active: boolean) {
  return active ? 'hx-guide-glow' : ''
}

/** 側欄／底欄：使用者還不在目標頁時，導覽項發光 */
export function shouldGlowNav(step: OnboardingStep | null, navTo: string, pathname: string) {
  if (!step) return false
  if (step === 'connection' && navTo === '/settings') return pathname !== '/settings'
  if (step === 'persona' && navTo === '/personas') return !pathname.startsWith('/personas')
  return false
}

/** 頁內操作區：使用者已在目標頁時，具體按鈕／卡片發光 */
export function shouldGlowInPage(
  step: OnboardingStep | null,
  targetStep: OnboardingStep,
  pathname: string,
) {
  if (step !== targetStep) return false
  if (targetStep === 'connection') {
    return pathname === '/settings' || pathname.includes('/connections')
  }
  if (targetStep === 'persona') return pathname.startsWith('/personas')
  return false
}

export function onboardingStepLabel(step: OnboardingStep) {
  if (step === 'account') return '先建立帳號'
  if (step === 'connection') return '完成連線'
  return '建立人設'
}
