import { BadRequestException, Injectable } from '@nestjs/common';
import { AttendanceImportService } from './attendance-import.service';
import { AttendanceService } from './attendance.service';

export type LessonAttendanceSyncMode = 'refresh' | 'finalize';

export interface LessonAttendanceSyncOptions {
  scheduleId: number;
  lessonDate: string;
  actorId: number;
  canManageAll: boolean;
  mode: LessonAttendanceSyncMode;
}

@Injectable()
export class LessonAttendanceSyncService {
  constructor(
    private readonly attendanceService: AttendanceService,
    private readonly importService: AttendanceImportService,
  ) {}

  async syncLesson(options: LessonAttendanceSyncOptions) {
    const { scheduleId, lessonDate, actorId, canManageAll, mode } = options;
    const { schedule } = await this.attendanceService.getLessonAttendance(scheduleId, lessonDate);
    const userIds = await this.attendanceService.getTeacherClassDingUserIds(
      actorId,
      schedule.classId!,
      canManageAll,
      lessonDate,
    );
    const dateRange = this.attendanceService.getLessonAttendanceImportDateRange(
      schedule,
      lessonDate,
    );
    const imported = await this.importService.importFromDingTalk({
      ...dateRange,
      userIds,
      autoMatch: true,
      userId: actorId,
    });
    if (!imported.success || imported.errors.length > 0) {
      throw new BadRequestException(imported.errors.join('; ') || '钉钉考勤拉取失败');
    }

    const attendance = await this.attendanceService.createLessonAttendanceFromDingTalk(
      scheduleId,
      lessonDate,
      actorId,
      mode === 'finalize',
    );
    return { attendance, imported: imported.imported, matched: imported.matched };
  }
}
