import { MigrationInterface, QueryRunner } from 'typeorm';

export class AttendanceRequests1781000000000 implements MigrationInterface {
  name = 'AttendanceRequests1781000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('attendance_requests');
    if (table) {
      return;
    }

    await queryRunner.query(`
      CREATE TABLE \`attendance_requests\` (
        \`id\` varchar(36) NOT NULL,
        \`owner_id\` varchar(191) NOT NULL,
        \`member_id\` varchar(191) NOT NULL,
        \`request_type\` enum('off_day_work','correction') NOT NULL,
        \`request_date\` datetime NOT NULL,
        \`reason\` varchar(191) NOT NULL DEFAULT 'NT/APL',
        \`status\` enum('pending','approved','rejected','cancelled') NOT NULL DEFAULT 'pending',
        \`requested_payload\` json NULL,
        \`attendance_id\` varchar(36) NULL,
        \`approver_type\` varchar(191) NULL,
        \`approver_owner_id\` varchar(191) NULL,
        \`approver_member_id\` varchar(191) NULL,
        \`approved_at\` datetime NULL,
        \`admin_note\` varchar(191) NULL,
        \`created_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
        \`updated_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
        PRIMARY KEY (\`id\`),
        INDEX \`IDX_attendance_requests_member_date_status\` (\`member_id\`, \`request_date\`, \`status\`),
        INDEX \`IDX_attendance_requests_owner_id\` (\`owner_id\`),
        INDEX \`IDX_attendance_requests_attendance_id\` (\`attendance_id\`)
      ) ENGINE=InnoDB
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('attendance_requests');
    if (!table) {
      return;
    }

    await queryRunner.query(`DROP TABLE \`attendance_requests\``);
  }
}
