import type { PlanFilterStatus, PlanQuickFilter } from '../../../features/planBoard';
import type { PlanSidebarKey, PlaySidebarKey } from '../types';

export const PLAN_GROUP_LABEL = '작업';

export const PLAN_FILTER_LABELS: Record<PlanFilterStatus, string> = {
  all: '자동화 현황',
  'in-progress': '실행 중 (0)',
  done: '완료',
  error: '실패 (0)',
};

export const DOCS_DEFAULT_FOLDER = 'worklogs';

export const DOCS_FOLDER_LABELS: Record<string, string> = {
  worklogs: '작업일지',
  features: '기능문서',
  components: '컴포넌트문서',
  templates: '문서템플릿',
};

export const PLAN_SIDEBAR_LABELS: Record<PlanSidebarKey, string> = {
  ...PLAN_FILTER_LABELS,
  release: 'release (0)',
  'release-review': 'release 검수',
  board: '작업 요청',
  charts: '차트',
  schedule: '스케줄',
  history: '이력',
  'automation-type': '자동화 유형',
  'automation-context': 'Context 유형',
  'server-command': 'Command',
};

export const PLAN_BASE_OPEN_KEYS = ['plan-group', 'server-group', 'codex-live-group', 'app-log-group'] as const;
export const PLAY_BASE_OPEN_KEYS = ['play-group', 'play-layout-group'] as const;
export const PLAY_LAYOUT_RECORD_PREFIX = 'layout-record:' as const;
export const PLAN_MENU_STATUS_ORDER: PlanFilterStatus[] = ['all', 'in-progress', 'error', 'done'];

export const PLAY_SIDEBAR_LABELS: Record<Extract<PlaySidebarKey, 'layout'>, string> = {
  layout: 'Layout Editor',
};

export const PLAN_MENU_ANCHOR_IDS: Partial<Record<PlanSidebarKey, string>> = {
  all: 'plan-menu-all',
  'in-progress': 'plan-menu-in-progress',
  release: 'plan-menu-release',
  'release-review': 'plan-menu-release-review',
  done: 'plan-menu-done',
  error: 'plan-menu-error',
  board: 'plan-menu-board',
  charts: 'plan-menu-charts',
  schedule: 'plan-menu-schedule',
  history: 'plan-menu-history',
  'automation-type': 'plan-menu-automation-type',
  'automation-context': 'plan-menu-automation-context',
  'server-command': 'plan-menu-server-command',
};

export function resolvePlanOpenKeys() {
  return [...PLAN_BASE_OPEN_KEYS];
}

export function resolvePlayOpenKeys() {
  return [...PLAY_BASE_OPEN_KEYS];
}

export function resolveSavedLayoutMenuKey(layoutId: string): PlaySidebarKey {
  return `${PLAY_LAYOUT_RECORD_PREFIX}${layoutId}`;
}

export function resolveSavedLayoutIdFromMenuKey(key: PlaySidebarKey) {
  return key.startsWith(PLAY_LAYOUT_RECORD_PREFIX) ? key.slice(PLAY_LAYOUT_RECORD_PREFIX.length) : null;
}

export function resolvePlaySidebarLabel(selectedPlayMenu: PlaySidebarKey, savedLayouts: Array<{ id: string; name: string }>) {
  const savedLayoutId = resolveSavedLayoutIdFromMenuKey(selectedPlayMenu);

  if (selectedPlayMenu === 'layout') {
    return PLAY_SIDEBAR_LABELS[selectedPlayMenu];
  }

  return savedLayouts.find((record) => record.id === savedLayoutId)?.name ?? 'Saved Layout';
}

export function renderPlanMenuLabel(menu: PlanSidebarKey, label: string) {
  const anchorId = PLAN_MENU_ANCHOR_IDS[menu];

  if (!anchorId) {
    return label;
  }

  return <span id={anchorId}>{label}</span>;
}

export function resolvePlanQuickFilterMenu(filter: PlanQuickFilter): Extract<PlanSidebarKey, 'in-progress' | 'release' | 'error'> {
  if (filter === 'working') {
    return 'in-progress';
  }

  return filter === 'release-pending-main' ? 'release' : 'error';
}

export function resolvePlanMenuState(menu: PlanSidebarKey) {
  if (menu === 'release') {
    return {
      quickFilter: 'release-pending-main' as PlanQuickFilter,
    };
  }

  return {
    quickFilter: null,
  };
}

export function getDocsSectionLabel(section: string) {
  return DOCS_FOLDER_LABELS[section] ?? section;
}
