Don’t know if it helps, but maybe see if any of what I posted in this other thread helps? This general approach (making a simple / self-contained case I can play around with with just the data structure and an output) has been really helpful for me in the past.
You could also see if merge() with maps and the spread operator helps at all for your use case (I haven’t looked too carefully at your exact case). But high level, sometimes with Terraform, half the battle is figuring out what the input data structure really should look like, and then figure out to wrangle it into the form you need it in (which is often different).