import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.sequelize.query(`DO $$
  BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'enum_approval_status') THEN
      CREATE TYPE enum_approval_status AS ENUM ('PENDING','IN_PROGRESS','APPROVED','REJECTED','SKIPPED');
    END IF;
  END$$;`);

  await queryInterface.createTable('approval_levels', {
    level_id: { type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4 },
    request_id: { type: DataTypes.UUID, allowNull: false, references: { model: 'workflow_requests', key: 'request_id' } },
    level_number: { type: DataTypes.INTEGER, allowNull: false },
    level_name: { type: DataTypes.STRING(100), allowNull: true },
    approver_id: { type: DataTypes.UUID, allowNull: false, references: { model: 'users', key: 'user_id' } },
    approver_email: { type: DataTypes.STRING(255), allowNull: false },
    approver_name: { type: DataTypes.STRING(200), allowNull: false },
    tat_hours: { type: DataTypes.DECIMAL(10,2), allowNull: false },
    tat_days: { type: DataTypes.INTEGER, allowNull: false },
    status: { type: 'enum_approval_status' as any, allowNull: false, defaultValue: 'PENDING' },
    level_start_time: { type: DataTypes.DATE, allowNull: true },
    level_end_time: { type: DataTypes.DATE, allowNull: true },
    action_date: { type: DataTypes.DATE, allowNull: true },
    comments: { type: DataTypes.TEXT, allowNull: true },
    rejection_reason: { type: DataTypes.TEXT, allowNull: true },
    is_final_approver: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false },
    elapsed_hours: { type: DataTypes.DECIMAL(10,2), allowNull: false, defaultValue: 0 },
    remaining_hours: { type: DataTypes.DECIMAL(10,2), allowNull: false, defaultValue: 0 },
    tat_percentage_used: { type: DataTypes.DECIMAL(5,2), allowNull: false, defaultValue: 0 },
    created_at: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
    updated_at: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
  });

  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "approval_levels_request_id" ON "approval_levels" ("request_id");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "approval_levels_approver_id" ON "approval_levels" ("approver_id");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "approval_levels_status" ON "approval_levels" ("status");');
  await queryInterface.sequelize.query(`DO $$
  BEGIN
    IF NOT EXISTS (
      SELECT 1 FROM pg_constraint WHERE conname = 'uq_approval_levels_request_level'
    ) THEN
      ALTER TABLE "approval_levels" ADD CONSTRAINT "uq_approval_levels_request_level" UNIQUE ("request_id", "level_number");
    END IF;
  END$$;`);
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('approval_levels');
  await queryInterface.sequelize.query('DROP TYPE IF EXISTS enum_approval_status;');
}


