export function toMinutes(time: string): number {
  const [hour, minute] = time.split(':').map(Number);
  return hour * 60 + minute;
}

export function getCourseClock(date: Date): { date: string; minutes: number } {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return {
    date: `${year}-${month}-${day}`,
    minutes: date.getHours() * 60 + date.getMinutes(),
  };
}

export function shiftDate(date: string, days: number): string {
  const d = new Date(`${date}T00:00:00.000Z`);
  d.setUTCDate(d.getUTCDate() + days);
  return d.toISOString().slice(0, 10);
}

export function mapLessonScheduleTimeToSession(startTime: string): string {
  const hour = parseInt(startTime.slice(0, 2), 10);
  if (hour < 8) return 'morning_reading';
  if (hour < 12) return 'morning';
  if (hour < 17) return 'afternoon';
  if (hour < 20) return 'evening_study';
  return 'night_check';
}

export function isClassStudentActiveOnDate(
  classStudent: Pick<
    import('../entities/class-student.entity').ClassStudent,
    'joinDate' | 'leaveDate' | 'status'
  >,
  lessonDate: string,
): boolean {
  const status = classStudent.status ?? 'active';
  if (!['active', 'left'].includes(status)) return false;
  if (classStudent.joinDate && classStudent.joinDate > lessonDate) return false;
  if (classStudent.leaveDate && classStudent.leaveDate < lessonDate) return false;
  return true;
}
