Assuming you are using Pokemon Essentials (as you don't specify and this is technically a page for any type of game deving) this is pretty easy to make. The first part is making your gym difficult as the second gym, making it how you would any other gym. Gym badges already don't have to be gathered in order, so you don't have to anything special to make it skippable.
Scaling is a little bit more difficult, but the simplest, most methodical way to do it is to make a new event page on the same event that you put the original gym trainers/leaders, then make the battle again, but make the teams stronger. Then make it depend on a switch that is activated when you receive the third gym badge. Repeat with all other badges.
You will have to make 7 different battle versions if you do it like this, so check out the Pokemon Essentials wiki if you don't know how to make multiple battles of the same trainer.
Note, that you will need to put the event page that is determined when Self-Switch A is ON to the last page, or the new battle pages will override it.