export type MenuItem = {
  key: string
  label: string
}

export type MenuSection = {
  id: string
  title: string
  items: MenuItem[]
}

/** 비즈케어홈즈(h.bizcare.co.kr) 실제 메뉴 — 경로 그대로 */
export function careMenuSections(): MenuSection[] {
  return [
    {
      id: 'home',
      title: '대시보드',
      items: [{ key: '/care/main', label: '대시보드' }],
    },
    {
      id: 'product',
      title: '상품관리',
      items: [
        { key: '/care/product/internet', label: '인터넷상품' },
        { key: '/care/product/home', label: '홈렌탈상품' },
        { key: '/care/product/log', label: '상품이력' },
      ],
    },
    {
      id: 'contract',
      title: '계약관리',
      items: [
        { key: '/care/contract/internet', label: '인터넷접수' },
        { key: '/care/contract/home', label: '홈렌탈접수' },
        { key: '/care/contract/log', label: '계약이력' },
        { key: '/care/contract/adjust', label: '일괄정책변경' },
        { key: '/care/contract/balance', label: '계약후정산' },
      ],
    },
    {
      id: 'reception',
      title: '접수관리',
      items: [
        { key: '/care/reception/list', label: '접수리스트' },
      ],
    },
    {
      id: 'invoice',
      title: '정산관리',
      items: [
        { key: '/care/invoice/publish', label: '정산서발행' },
        { key: '/care/invoice/my', label: '나의정산서' },
        { key: '/care/invoice/log', label: '정산서이력' },
      ],
    },
    {
      id: 'pending',
      title: '일정관리',
      items: [
        { key: '/care/pending/calendar', label: '캘린더' },
        { key: '/care/pending/pending', label: '일정관리' },
        { key: '/care/pending/log', label: '일정이력' },
      ],
    },
    {
      id: 'booking',
      title: '예약관리',
      items: [
        { key: '/care/booking/booking', label: '예약관리' },
        { key: '/care/booking/log', label: '예약이력' },
      ],
    },
    {
      id: 'customer',
      title: '고객관리',
      items: [
        { key: '/care/customer/customer', label: '고객관리' },
        { key: '/care/customer/log', label: '고객이력' },
      ],
    },
    {
      id: 'abooks',
      title: '장부관리',
      items: [
        { key: '/care/abooks/abooks-mon', label: '간편장부' },
        { key: '/care/abooks/log', label: '장부이력' },
      ],
    },
    {
      id: 'report',
      title: '통계관리',
      items: [
        { key: '/care/report/daily', label: '일일리포트' },
        { key: '/care/report/monthly', label: '월간리포트' },
        { key: '/care/report/internet', label: '인터넷통계' },
        { key: '/care/report/home', label: '홈렌탈통계' },
        { key: '/care/report/member', label: '직원별통계' },
        { key: '/care/report/agency', label: '거래처별통계' },
        { key: '/care/report/incentive', label: '인센티브계산' },
      ],
    },
    {
      id: 'setting',
      title: '환경설정',
      items: [
        { key: '/care/setting/agency', label: '거래처관리' },
        { key: '/care/setting/member', label: '직원관리' },
        { key: '/care/setting/preference', label: '기본설정' },
        { key: '/care/setting/level', label: '권한설정' },
        { key: '/care/setting/partner1', label: '상부점관리' },
        { key: '/care/setting/partner2', label: '하부점관리' },
      ],
    },
    {
      id: 'cs',
      title: '고객센터',
      items: [
        { key: '/care/cs/notice', label: '공지사항' },
        { key: '/care/cs/question', label: '사용문의' },
      ],
    },
    {
      id: 'bbs',
      title: '사내게시판',
      items: [{ key: '/care/bbs/bbs', label: '사내게시판' }],
    },
  ]
}

/** @deprecated 호환용 */
export function agentMenuSections(): MenuSection[] {
  return careMenuSections()
}

export function shopMenuSections(): MenuSection[] {
  return [
    {
      id: 'shop',
      title: '판매점',
      items: [
        { key: '/shop/notice', label: '공지사항' },
        { key: '/shop/policy', label: '정책정보' },
        { key: '/shop/customer', label: '고객조회' },
        { key: '/shop/contract', label: '계약조회' },
      ],
    },
  ]
}

/** 권한설정·노출설정에서 허용된 섹션 id만 남김. allowedIds가 null이면 전부 표시(로딩 전). */
export function filterMenuSections(
  sections: MenuSection[],
  allowedIds: string[] | null | undefined,
): MenuSection[] {
  if (!allowedIds) return sections
  const set = new Set(allowedIds)
  return sections.filter((sec) => set.has(sec.id))
}

export const SITEMAP_TITLE_TO_ID: Record<string, string> = {
  대시보드: 'home',
  상품관리: 'product',
  계약관리: 'contract',
  접수관리: 'reception',
  정산관리: 'invoice',
  일정관리: 'pending',
  예약관리: 'booking',
  고객관리: 'customer',
  장부관리: 'abooks',
  통계관리: 'report',
  환경설정: 'setting',
  고객센터: 'cs',
  사내게시판: 'bbs',
  게시판: 'bbs',
}

export function resolveSelectedKey(pathname: string): string {
  if (pathname.startsWith('/care/contract/write')) {
    const q = typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('type') : null
    if (q === 'home') return '/care/contract/home'
    return '/care/contract/internet'
  }
  if (pathname === '/care/sitemap') return '/care/main/sitemap'
  return pathname
}

export function sectionContainsPath(section: MenuSection, pathname: string): boolean {
  const selected = resolveSelectedKey(pathname)
  return section.items.some((it) => selected === it.key || selected.startsWith(`${it.key}/`))
}
