export interface TaskWorklog {
  id: string;
  taskId: string;
  userId: string;
  date: string;
  hours: number;
  workContent: string;
  createdAt: string;
}

export function calcActualHours(worklogs: TaskWorklog[], taskId: string): number {
  return worklogs
    .filter((w) => w.taskId === taskId)
    .reduce((sum, w) => sum + w.hours, 0);
}

export function getTaskWorklogs(worklogs: TaskWorklog[], taskId: string): TaskWorklog[] {
  return worklogs
    .filter((w) => w.taskId === taskId)
    .sort((a, b) => b.date.localeCompare(a.date));
}

export function getUserDailyWorklogs(worklogs: TaskWorklog[], userId: string, date: string): TaskWorklog[] {
  return worklogs.filter((w) => w.userId === userId && w.date === date);
}

export function getUserDailySummary(worklogs: TaskWorklog[], userId: string, date: string): { total: number; items: { taskId: string; hours: number; workContent: string }[] } {
  const items = getUserDailyWorklogs(worklogs, userId, date);
  return {
    total: items.reduce((sum, w) => sum + w.hours, 0),
    items: items.map((w) => ({ taskId: w.taskId, hours: w.hours, workContent: w.workContent })),
  };
}
