// ai_domain 값을 @theme의 bg-domain-{slug} 토큰 슬러그로 매핑.
// 백엔드는 'Knowledge/Philosophy', 'Knowledge/Industrial_Safety', 'Reference', null
// 형태를 모두 보낼 수 있으므로 leaf만 잘라 lowercase하고 industrial_safety→safety로 재매핑.
//
// Phase C: 대시보드 today_by_domain 누적바, recent_documents accent에서 사용.
// 추후 phase에서도 도메인 색상 칩이 필요해지면 여기로 일원화.

export type DomainSlug =
  | 'philosophy'
  | 'language'
  | 'engineering'
  | 'safety'
  | 'programming'
  | 'general'
  | 'reference';

const LEAF_TO_SLUG: Record<string, DomainSlug> = {
  philosophy: 'philosophy',
  language: 'language',
  engineering: 'engineering',
  industrial_safety: 'safety',
  safety: 'safety',
  programming: 'programming',
  general: 'general',
  reference: 'reference',
};

/** 'Knowledge/Philosophy' → 'philosophy', 'Reference' → 'reference', null → null */
export function domainSlug(domain: string | null | undefined): DomainSlug | null {
  if (!domain) return null;
  const leaf = domain.includes('/') ? domain.split('/').pop()! : domain;
  return LEAF_TO_SLUG[leaf.toLowerCase()] ?? null;
}

const SLUG_BG_CLASS: Record<DomainSlug, string> = {
  philosophy: 'bg-domain-philosophy',
  language: 'bg-domain-language',
  engineering: 'bg-domain-engineering',
  safety: 'bg-domain-safety',
  programming: 'bg-domain-programming',
  general: 'bg-domain-general',
  reference: 'bg-domain-reference',
};

/** bg-domain-{slug} 클래스. 미지정/미매핑은 bg-default. */
export function domainBgClass(domain: string | null | undefined): string {
  const slug = domainSlug(domain);
  return slug ? SLUG_BG_CLASS[slug] : 'bg-default';
}

const SLUG_LABEL: Record<DomainSlug, string> = {
  philosophy: 'Philosophy',
  language: 'Language',
  engineering: 'Engineering',
  safety: 'Industrial Safety',
  programming: 'Programming',
  general: 'General',
  reference: 'Reference',
};

/** 표시용 라벨. domain이 null이면 '미분류'. */
export function domainLabel(domain: string | null | undefined): string {
  const slug = domainSlug(domain);
  if (!slug) return domain ?? '미분류';
  return SLUG_LABEL[slug];
}
