Argh! Half of the information is not visible due to camera angle. :<
Before doing anything, verify power to the branch circuit is off. Turn the light on, verify the bulb is illuminated. Kill the breaker, verify the bulb is OFF. Make sure no one mucks with the breaker hereafter!
[Note, I suspect at least ne other outlet/fixture is powered AFTER this point; see notes below. You might want to identify that so you know how leisurely your repair can be -- before "someone" gets annoyed that they are reading in the dark, etc.]It looks like the ROMEX at top right is bringing power *to* the box (or, taking it FROM the box and carrying it off to some other Jbox down the line). There appear to be *two* more ROMEX (?) lines not visible behind the fam mounting bracket at the lower left. One of these will be in a similar role as the ROMEX in the upper right (i.e., bringing power in or carrying it farther down the line).
The *other* (not visible) ROMEX will be the drop to the wall switch. It currently has it's white wire connected to the two blacks (and blue). This supplies power (hot) to the wall switch.
[N.B. This white wire should be "taped black" -- i.e., have a piece of black electrical tape wrapped around it to indicate "this wire is serving the role of a BLACK/hot wire in this circuit"]After passing through the switch, this wire returns as the mating BLACK wire in that strand of ROMEX and is connected to the black pigtail of the appliance -- i.e., the FAN connection (from your description of the circuit's operation).
The LIGHT gets power through the blue pigtail -- i.e., ALWAYS (as it bypasses the switch entirely).
FAN and LIGHT share the white, common "neutral" pigtail (which is always connected to the neutrals from the ROMEX's that are passing
*through* the box.If you want the FAN *and* LIGHT to both be "gated" by the wall switch (i.e., neither will operate unless the switch is ON), then MOVE the blue pigtail to the wire nut that has the black pigtail (so, that wirenut will now have black and blue pigtails AND the "switched black" lead returning from the wall switch).
Don't just naively add the blue wire to the black/black wires. Make sure the wire hasn't degraded from being twisted in its previous connection, now untwisted, etc. You may have to trim off any damaged ends and remove some more insulation to have "good wire" to play with.
Make sure the two blacks and white-taped-black are also firmly twisted back together before reattaching the wire nut.
Make sure none of the wires will end up pinched in the box or by the fan bracket! Remember, fan is designed to wobble in operation...
N.B. You can often rewire the *appliance* as well. So, you can arrange for the pull chain to control the FAN and have the LIGHT be "on all the time" (i.e., whenever the wall switch is ON, the light comes on; the fan can also be gated on/off through the switch OR can have power available at all times and turned on/off via the pull chain)
[This latter is how I prefer to have ceiling fans wired; I *always* want the light to be easily available and will suffer the inconvenience of having to pull a chain for the fan!]Do yourself a favor and put a cable clamp on the piece of ROMEX coming in from the top right. If the one (two?) at the bottom left is missing a clamp as well, add one there. You may have to remove the box (at least two wood screws visible) to get it *into* the knockout on the Jbox... and, may have a bit of work required to get the whole assembly back up into the ceiling without enlarging the hole in the drywall. Can't attest to the stiffness of the support (to which the box AND fan support) are connected but fans like to introduce vibrations while operating; you don't want to discover that you're slowly wearing a hole through the insulation where that ROMEX passes through the knockout (rough edge of Jbox opening).
Also, verify the ground (bare) wire from the "invisible" ROMEX's are actually present and bonded to the case -- and the other ground wire. Otherwise, something is not seeing a ground!