import type { SectionKey } from './sections';
import { taskBelongsToSection } from './sections';

/** 4분면 보드 슬롯 (preview 레이아웃) */
export type BoardSlotId = 'hrm' | 'hrd' | 'ex' | 'ga';

export interface BoardSlotConfig {
  id: BoardSlotId;
  cssClass: `dept-card--${BoardSlotId}`;
  /** API 컬럼 키 — null이면 더미 슬롯(로컬 헤더·section 문자열) */
  sectionKey: SectionKey | null;
  /** 더미 슬롯·API 미연동 시 task.section 값 */
  dummySection?: string;
  defaultTitle: string;
  defaultTitleEn: string;
  accent: string;
}

export const BOARD_SLOTS: BoardSlotConfig[] = [
  {
    id: 'hrm',
    cssClass: 'dept-card--hrm',
    sectionKey: '인사관리',
    defaultTitle: '인사관리',
    defaultTitleEn: 'HRM',
    accent: '#29724f',
  },
  {
    id: 'hrd',
    cssClass: 'dept-card--hrd',
    sectionKey: '학습성장',
    defaultTitle: '인재육성',
    defaultTitleEn: 'HRD',
    accent: '#37a184',
  },
  {
    id: 'ex',
    cssClass: 'dept-card--ex',
    sectionKey: null,
    dummySection: '조직문화',
    defaultTitle: '조직문화',
    defaultTitleEn: 'EX',
    accent: '#4a9480',
  },
  {
    id: 'ga',
    cssClass: 'dept-card--ga',
    sectionKey: '운영관리',
    defaultTitle: '총무관리',
    defaultTitleEn: 'GA',
    accent: '#0d4a38',
  },
];

export function slotSectionLabel(slot: BoardSlotConfig): string {
  return slot.sectionKey ?? slot.dummySection ?? slot.defaultTitle;
}

/** 조직문화(EX) 슬롯 — HR 원본 category와 무관하게 배치할 프로젝트 */
const EX_CULTURE_TITLE = /회사생활|C\.E\.L|조직문화|복리후생|문화\s*진단|직원\s*소통/i;

export function isExCultureTask(task: {
  section?: string | null;
  title?: string | null;
  tag?: string | null;
}): boolean {
  if (task.section?.trim() === '조직문화') return true;
  if (task.tag === 'Culture') return true;
  const title = task.title?.trim() ?? '';
  return title.length > 0 && EX_CULTURE_TITLE.test(title);
}

export function resolveTaskBoardSlot(task: {
  section?: string | null;
  title?: string | null;
  tag?: string | null;
}): BoardSlotId | null {
  if (isExCultureTask(task)) return 'ex';
  for (const slot of BOARD_SLOTS) {
    if (slot.id === 'ex') continue;
    if (taskBelongsToSlot(task.section, slot)) return slot.id;
  }
  return null;
}

export function taskBelongsToBoardSlot(
  task: { section?: string | null; title?: string | null; tag?: string | null },
  slot: BoardSlotConfig,
): boolean {
  return resolveTaskBoardSlot(task) === slot.id;
}

/** 컬럼 헤더 — API 기본값(운영관리 등)은 슬롯 표시명(총무관리 등)으로 */
export function columnDisplayTitle(
  slot: BoardSlotConfig,
  colConfig?: { title?: string | null } | null,
  dummyHeader?: { title?: string } | null,
): string {
  if (!slot.sectionKey) {
    return dummyHeader?.title?.trim() || slot.defaultTitle;
  }
  const custom = colConfig?.title?.replace(/\s*부문$/, '').trim();
  if (!custom) return slot.defaultTitle;
  if (custom === slot.sectionKey || custom === '운영관리' || custom === 'HR' || custom === '학습성장') {
    return slot.defaultTitle;
  }
  return custom;
}

export function columnDisplayTitleEn(
  slot: BoardSlotConfig,
  colConfig?: { titleEn?: string | null } | null,
  dummyHeader?: { titleEn?: string } | null,
): string {
  if (!slot.sectionKey) {
    return dummyHeader?.titleEn?.trim() || slot.defaultTitleEn;
  }
  const custom = colConfig?.titleEn?.trim();
  if (!custom || custom === 'Operations' || custom === 'Human Resources') {
    return slot.defaultTitleEn;
  }
  return custom;
}

export function taskBelongsToSlot(
  taskSection: string | null | undefined,
  slot: BoardSlotConfig,
): boolean {
  if (!taskSection) return false;
  const label = slotSectionLabel(slot);
  if (taskSection.trim() === label) return true;
  if (slot.sectionKey && taskBelongsToSection(taskSection, slot.sectionKey)) return true;
  return false;
}

export const BOARD_SLOT_ORDER: BoardSlotId[] = ['hrm', 'hrd', 'ex', 'ga'];

export function getBoardSlot(id: BoardSlotId): BoardSlotConfig {
  return BOARD_SLOTS.find((s) => s.id === id)!;
}
