Well secondary attacks is something that I wanted for a while. But basically you can edit the attack component to use an inventory component and maybe when say ranged inventory is empty if the unit has some special component let's say SecondaryAttack then make it upgrade to the melee version of it. Once you somehow fill the inventory again through auras or trickle or whatever the secondary attack might switch back. You can also do it trhough the gui. That's how i'd imagine it.
Obviously that doesn't account for enemy proximity and other fancy stuff.
Usually it's because there is a stronghold placement type that's being set in the map options. But it might be persist match settings acting out again. CC @phosit