import { type IssueResponse } from '@/client';

// ---------------------------------------------------------------------------
// Status config
// ---------------------------------------------------------------------------

export const ALL_STATUSES = ['open', 'closed'] as const;
export type IssueStatus = (typeof ALL_STATUSES)[number];

export const STATUS_CONFIG: Record<
  IssueStatus,
  {
    label: string;
    columnBg: string;
    badgeBg: string;
    badgeText: string;
    openIcon: string;
  }
> = {
  open: {
    label: 'Open',
    columnBg: 'bg-emerald-50/50 dark:bg-emerald-950/20',
    badgeBg: 'bg-emerald-100 dark:bg-emerald-900/50',
    badgeText: 'text-emerald-700 dark:text-emerald-300',
    openIcon: '○',
  },
  closed: {
    label: 'Closed',
    columnBg: 'bg-violet-50/50 dark:bg-violet-950/20',
    badgeBg: 'bg-violet-100 dark:bg-violet-900/50',
    badgeText: 'text-violet-700 dark:text-violet-300',
    openIcon: '●',
  },
};

// ---------------------------------------------------------------------------
// Priority config (current backend doesn't have priority, use label text)
// ---------------------------------------------------------------------------

export const PRIORITY_ORDER = ['high', 'medium', 'low'] as const;
export type IssuePriority = (typeof PRIORITY_ORDER)[number];

export const PRIORITY_CONFIG: Record<
  IssuePriority,
  { label: string; color: string; bg: string }
> = {
  high: { label: 'High', color: 'text-red-500', bg: 'bg-red-100 dark:bg-red-900/50' },
  medium: { label: 'Medium', color: 'text-yellow-500', bg: 'bg-yellow-100 dark:bg-yellow-900/50' },
  low: { label: 'Low', color: 'text-blue-500', bg: 'bg-blue-100 dark:bg-blue-900/50' },
};

// ---------------------------------------------------------------------------
// View mode
// ---------------------------------------------------------------------------

export type ViewMode = 'board' | 'list';

// ---------------------------------------------------------------------------
// Helper
// ---------------------------------------------------------------------------

export type RepoLike = {
  uid?: string;
  repo_uid?: string;
  repo_name: string;
  namespace?: string;
  description?: string | null;
  is_private?: boolean;
  star_count?: number;
  watch_count?: number;
  commit_count?: number;
  branch_count?: number;
  tag_count?: number;
  default_branch?: string;
  last_commit_at?: string;
};

export type IssueListResponseLike = {
  items?: IssueResponse[];
  issues?: IssueResponse[];
  total?: number;
};

export type MultiFilter = {
  statuses: string[];
  search: string;
};

export const STATUS_OPTIONS = ['open', 'closed'];

export const EMPTY_FILTER: MultiFilter = {
  statuses: [],
  search: '',
};

export const normalizeIssueList = (
  value: unknown,
): { items: IssueResponse[]; total: number } => {
  if (Array.isArray(value))
    return { items: value as IssueResponse[], total: value.length };
  const payload = (value || {}) as IssueListResponseLike;
  const items = payload.items || payload.issues || [];
  return { items, total: payload.total ?? items.length };
};

export const getRepoUid = (repo: RepoLike): string =>
  repo.uid || repo.repo_uid || repo.repo_name;

export const getViewStorageKey = (projectName: string) =>
  `project-issues-saved-views:${projectName}`;

export function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleDateString('en-US', {
    month: 'short',
    day: 'numeric',
  });
}
