import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { DataSource } from 'typeorm';
import {
  cleanupDepositRefundColumns,
  ensureAttendanceDevicesSchema,
  ensureStudentProfileCollegeColumns,
  ensureStudentWalletSchema,
  ensureSyncStateLeaseColumns,
  normalizeClassroomStatuses,
  removeUnusedClassroomColumns,
  removeUnusedClassStudentColumns,
  removeUnusedRoomColumns,
} from './database-migrations.schema';
import { ensureAiConfigTable } from './database-migrations.ai';
import {
  ensureCourseAttendanceSchema,
  ensureDingLeaveSchema,
  protectAttendanceHistory,
} from './database-migrations.attendance';
import { backfillOrganizations, normalizeClassDates } from './database-migrations.backfill';

@Injectable()
export class DatabaseMigrationsService implements OnApplicationBootstrap {
  private readonly logger = new Logger(DatabaseMigrationsService.name);

  constructor(private readonly dataSource: DataSource) {}

  async onApplicationBootstrap(): Promise<void> {
    await this.ensureAiConfigTable();
    await this.ensureSyncStateLeaseColumns();
    await this.ensureStudentProfileCollegeColumns();
    await this.ensureCourseAttendanceSchema();
    await this.ensureDingLeaveSchema();
    await this.ensureAttendanceDevicesSchema();
    await this.ensureStudentWalletSchema();
    await this.backfillOrganizations();
    await this.normalizeClassDates();
    await this.protectAttendanceHistory();
    await this.removeUnusedClassroomColumns();
    await this.removeUnusedRoomColumns();
    await this.cleanupDepositRefundColumns();
    await this.removeUnusedClassStudentColumns();
    await this.normalizeClassroomStatuses();
  }

  async ensureSyncStateLeaseColumns(): Promise<void> {
    return ensureSyncStateLeaseColumns(this.dataSource);
  }

  async ensureStudentProfileCollegeColumns(): Promise<void> {
    return ensureStudentProfileCollegeColumns(this.dataSource);
  }

  async ensureAttendanceDevicesSchema(): Promise<void> {
    return ensureAttendanceDevicesSchema(this.dataSource);
  }

  async ensureStudentWalletSchema(): Promise<void> {
    return ensureStudentWalletSchema(this.dataSource);
  }

  async removeUnusedClassroomColumns(): Promise<void> {
    return removeUnusedClassroomColumns(this.dataSource);
  }

  async removeUnusedRoomColumns(): Promise<void> {
    return removeUnusedRoomColumns(this.dataSource);
  }

  async cleanupDepositRefundColumns(): Promise<void> {
    return cleanupDepositRefundColumns(this.dataSource);
  }

  async removeUnusedClassStudentColumns(): Promise<void> {
    return removeUnusedClassStudentColumns(this.dataSource);
  }

  async normalizeClassroomStatuses(): Promise<void> {
    return normalizeClassroomStatuses(this.dataSource);
  }

  async ensureAiConfigTable(): Promise<void> {
    return ensureAiConfigTable(this.dataSource, this.logger);
  }

  async ensureCourseAttendanceSchema(): Promise<void> {
    return ensureCourseAttendanceSchema(this.dataSource, this.logger);
  }

  async ensureDingLeaveSchema(): Promise<void> {
    return ensureDingLeaveSchema(this.dataSource, this.logger);
  }

  async backfillOrganizations(): Promise<void> {
    return backfillOrganizations(this.dataSource);
  }

  async normalizeClassDates(): Promise<void> {
    return normalizeClassDates(this.dataSource, this.logger);
  }

  async protectAttendanceHistory(): Promise<void> {
    return protectAttendanceHistory(this.dataSource, this.logger);
  }
}
