import { getAllTodos } from '../storage';

const CATEGORY_LABELS: Record<string, string> = {
  feature: 'FEATURE',
  bug: 'BUG',
  refactor: 'REFACTOR',
  chore: 'CHORE',
  docs: 'DOCS',
  test: 'TEST',
};

const CATEGORY_ORDER = ['feature', 'bug', 'refactor', 'chore', 'docs', 'test'];

export function handleList(): void {
  const todos = getAllTodos();
  if (todos.length === 0) {
    console.log('No todos found.');
    return;
  }

  const sorted = [...todos].sort((a, b) => b.createdAt.localeCompare(a.createdAt));

  const grouped: Record<string, typeof todos> = {};
  for (const cat of CATEGORY_ORDER) {
    grouped[cat] = [];
  }
  for (const todo of sorted) {
    grouped[todo.category]?.push(todo);
  }

  for (const cat of CATEGORY_ORDER) {
    const items = grouped[cat];
    if (!items || items.length === 0) continue;

    console.log(`\x1b[1m${CATEGORY_LABELS[cat]}\x1b[0m`);
    for (const todo of items) {
      const status = todo.completed ? '\x1b[32m✓\x1b[0m' : '\x1b[31m○\x1b[0m';
      console.log(`  ${status} #${todo.id} ${todo.description}`);
    }
  }
}
