/*
 * 브레드크럼 경로 구성 — 라우트 → 크럼 트레일(Home 제외; Breadcrumb 가 Home 을 앞에 붙임).
 *
 * 메뉴 IA(AppShell GROUPS)와 정합하는 정적 매핑 + 상세/스코프 라우트 동적 패턴.
 * 라벨은 i18n 키(shell.menu.groups.* · shell.menu.items.* · breadcrumb.*)로 해소하고,
 * 미해소 시 label 폴백(ko)을 쓴다. 그룹 크럼은 라우트가 없는 아코디언 섹션이라 비링크,
 * 부모 목록/대시보드 크럼은 to 로 복귀 링크를 보장한다(상세→목록/메인 복귀).
 *
 * mdiLabels 만 import 하여 순환(AppShell → Breadcrumb → config → AppShell)을 피한다.
 */
import { labelForPath } from './mdiLabels';

export interface Crumb {
  /** i18n 키(우선). */
  labelKey?: string;
  /** 리터럴 라벨(i18n 미해소 폴백). */
  label?: string;
  /** 클릭 이동 경로(없으면 비링크 텍스트 — 그룹 섹션 등). */
  to?: string;
}

/** 그룹(대분류) 크럼 — 라우트 없음(아코디언 섹션). */
const G = (g: string): Crumb => ({ labelKey: `shell.menu.groups.${g}` });
/** 메뉴 항목 크럼 — to 지정 시 링크. */
const I = (i: string, to?: string): Crumb => ({ labelKey: `shell.menu.items.${i}`, to });
/** breadcrumb.* 전용 라벨(상세 화면 등 메뉴에 없는 라벨). */
const B = (k: string): Crumb => ({ labelKey: `breadcrumb.${k}` });

/** 정적(정확 일치) 경로 → 트레일. */
const STATIC: Record<string, Crumb[]> = {
  // 전시 운영
  '/exhibitors': [G('ops'), I('exhibitors')],
  '/schedule': [G('ops'), I('schedule')],
  '/halls': [G('ops'), I('halls')],
  '/booth-sales': [G('ops'), I('boothSales')],
  '/ops/operations': [G('ops'), I('operations')],

  // 설계·시공
  '/docs': [G('design'), I('documents')],
  '/docs/authoring': [G('design'), I('documents', '/docs'), B('docsAuthoring')],
  '/auctions': [G('design'), I('auctions')],
  '/contractor/dashboard': [G('design'), I('contractor')],
  '/logistics': [G('design'), I('logistics')],

  // 관람객·마케팅
  '/visitors': [G('visitor'), I('visitors')],
  '/visitors/checkin': [G('visitor'), I('visitors', '/visitors'), I('checkin')],
  '/leads': [G('visitor'), I('leads')],
  '/campaigns': [G('visitor'), I('campaigns')],
  '/sponsorship': [G('visitor'), I('sponsorship')],
  '/cms': [G('visitor'), I('cms')],
  '/cms/microsite': [G('visitor'), I('cms', '/cms'), B('micrositeBuilder')],
  '/cms/i18n': [G('visitor'), I('cms', '/cms'), B('i18nCms')],

  // 정산·분석
  '/settlement': [G('finance'), I('settlement')],
  '/analytics': [G('finance'), I('analytics')],

  // 업무 공통
  '/work/worklog': [G('work'), I('worklog')],
  '/work/schedule': [G('work'), I('workSchedule')],
  '/work/message': [G('work'), I('message')],
  '/work/notice': [G('work'), I('notice')],
  '/work/meeting': [G('work'), I('meeting')],
  '/work/report': [G('work'), I('report')],
  '/work/approval': [G('work'), I('approval')],
  '/work/opinion': [G('work'), I('opinion')],
  '/work/search': [G('work'), I('search')],
  '/app-qr': [G('work'), I('appqr')],

  // 개인·알림
  '/notifications': [B('notifications')],
  '/me': [B('me')],

  // 시스템관리 (상위 링크 = 관리 대시보드 /admin)
  '/admin': [G('system'), I('admin')],
  '/admin/login-slides': [G('system'), I('admin', '/admin'), I('slides')],
  '/admin/audit': [G('system'), I('admin', '/admin'), I('audit')],
  '/admin/settings': [G('system'), I('admin', '/admin'), I('settings')],
  '/admin/masterdata/rulesets': [G('system'), I('admin', '/admin'), I('rulesets')],
  '/admin/tenants': [G('system'), I('admin', '/admin'), I('tenants')],
  '/admin/users': [G('system'), I('admin', '/admin'), I('users')],
  '/admin/roles': [G('system'), I('admin', '/admin'), I('roles')],
  '/admin/codes': [G('system'), I('admin', '/admin'), I('codes')],
  '/admin/menus': [G('system'), I('admin', '/admin'), I('menus')],
  '/admin/depts': [G('system'), I('admin', '/admin'), I('depts')],
  '/admin/companies': [G('system'), I('admin', '/admin'), I('companies')],
  '/admin/programs': [G('system'), I('admin', '/admin'), I('programs')],
  '/admin/role-menus': [G('system'), I('admin', '/admin'), I('roleMenus')],
  '/admin/auth-policy': [G('system'), I('admin', '/admin'), I('authPolicy')],
  '/admin/login-history': [G('system'), I('admin', '/admin'), I('loginHistory')],
  '/admin/error-log': [G('system'), I('admin', '/admin'), I('errorLog')],
  '/admin/system-health': [G('system'), I('admin', '/admin'), I('systemHealth')],
  '/admin/mail-config': [G('system'), I('admin', '/admin'), I('mailConfig')],
  '/admin/notify-config': [G('system'), I('admin', '/admin'), I('notifyConfig')],
  '/admin/messages': [G('system'), I('admin', '/admin'), I('msgTemplates')],
  '/admin/holidays': [G('system'), I('admin', '/admin'), I('holidays')],
};

/** 동적 경로 패턴 → 트레일(구체적 규칙 우선). */
const PATTERNS: { re: RegExp; trail: Crumb[] }[] = [
  { re: /^\/auctions\/[^/]+\/award$/, trail: [G('design'), I('auctions', '/auctions'), B('auctionAward')] },
  { re: /^\/auctions\/[^/]+$/, trail: [G('design'), I('auctions', '/auctions'), B('auctionDetail')] },
  { re: /^\/events\/[^/]+\/dashboard$/, trail: [G('ops'), I('dashboard')] },
  { re: /^\/events\/[^/]+\/booths\/[^/]+\/home$/, trail: [B('exhibitorHome')] },
  { re: /^\/events\/[^/]+\/halls\/[^/]+\/layout$/, trail: [G('design'), I('floorplan')] },
  { re: /^\/events\/[^/]+\/halls\/[^/]+\/comparison$/, trail: [G('design'), B('comparison')] },
  { re: /^\/events\/[^/]+\/booths\/[^/]+\/design$/, trail: [G('design'), B('design')] },
  { re: /^\/events\/[^/]+\/booths\/[^/]+\/utility\/order$/, trail: [G('design'), B('utilityOrder')] },
  { re: /^\/events\/[^/]+\/booths\/[^/]+\/utility$/, trail: [G('design'), B('utility')] },
  { re: /^\/events\/[^/]+\/booths\/[^/]+\/compliance$/, trail: [G('design'), B('compliance')] },
  { re: /^\/events\/[^/]+\/halls\/[^/]+\/approvals$/, trail: [G('ops'), B('approvals')] },
  { re: /^\/events\/[^/]+\/halls\/[^/]+\/booths\/[^/]+\/review$/, trail: [G('ops'), B('reviewDetail')] },
  { re: /^\/events\/[^/]+\/booths\/[^/]+\/gallery$/, trail: [G('design'), B('gallery')] },
];

/** 라우트 → 크럼 트레일(Home 제외). 미등록 경로는 라우트 라벨 1개로 폴백. */
export function crumbTrail(pathname: string): Crumb[] {
  const clean = pathname.split('?')[0].split('#')[0];
  if (STATIC[clean]) return STATIC[clean];
  for (const { re, trail } of PATTERNS) {
    if (re.test(clean)) return trail;
  }
  return [{ label: labelForPath(clean) }];
}
