import type { Task, TaskStatus } from '../types';
import { hasVisibleIssue, type IssueVisibilityOptions } from './taskIssues';

/** DashboardHeader STAT_ACCENT 와 동일 */
export const TASK_STAT_COLORS = {
  IN_PROGRESS: '#10b981',
  REVIEW: '#ff9f0a',
  TODO: '#ff9f0a',
  CANCELLED: '#ff9f0a',
  DONE: '#b0b0b0',
} as const satisfies Record<TaskStatus, string>;

export interface DonutDisplay {
  pct: number;
  ringColor: string;
  label: string;
  labelColor: string;
  ariaLabel: string;
}

export function getProjectTitleStatusClass(
  task: Pick<Task, 'status' | 'showIssue' | 'issueNote' | 'issueEntries'>,
  options?: IssueVisibilityOptions,
): string {
  if (hasVisibleIssue(task, options)) return 'project-sub-title--issue';
  switch (task.status) {
    case 'IN_PROGRESS':
      return 'project-sub-title--in-progress';
    case 'REVIEW':
    case 'CANCELLED':
    case 'TODO':
      return 'project-sub-title--hold';
    case 'DONE':
      return 'project-sub-title--done';
    default:
      return 'project-sub-title--in-progress';
  }
}

export function getDonutDisplay(task: Pick<Task, 'status' | 'progress'>): DonutDisplay {
  const p = Math.min(100, Math.max(0, task.progress));

  switch (task.status) {
    case 'IN_PROGRESS':
      return {
        pct: p,
        ringColor: TASK_STAT_COLORS.IN_PROGRESS,
        label: `${p}%`,
        labelColor: TASK_STAT_COLORS.IN_PROGRESS,
        ariaLabel: `진행 ${p}%`,
      };
    case 'TODO':
      return {
        pct: 0,
        ringColor: TASK_STAT_COLORS.TODO,
        label: '대기',
        labelColor: TASK_STAT_COLORS.TODO,
        ariaLabel: '대기',
      };
    case 'REVIEW':
    case 'CANCELLED':
      return {
        pct: p,
        ringColor: TASK_STAT_COLORS.REVIEW,
        label: '보류',
        labelColor: TASK_STAT_COLORS.REVIEW,
        ariaLabel: p > 0 ? `보류 ${p}%` : '보류',
      };
    case 'DONE':
      return {
        pct: 100,
        ringColor: TASK_STAT_COLORS.DONE,
        label: '완료',
        labelColor: TASK_STAT_COLORS.DONE,
        ariaLabel: '완료',
      };
    default:
      return {
        pct: p,
        ringColor: TASK_STAT_COLORS.IN_PROGRESS,
        label: `${p}%`,
        labelColor: TASK_STAT_COLORS.IN_PROGRESS,
        ariaLabel: `진행률 ${p}%`,
      };
  }
}
