Headliner starting to un-adhere from ceiling near window

Hi guys, I have about 6" of headliner (started out as only .5") near the window that is separating from the ceiling near the window. I suppose wind entering the vehicle is causing it to separate and I'd like to fix it before it causes the whole headliner to come off. If I look closely at the ceiling i can see that the headliner has begun to separate from underneath the plastic molding which is probally why this is happening (i dont know if i'll be able to fix that) but hopefully someone has been here before and can help save my ceiling from looking like the sky is falling.

The underlying foam is starting to deteriorate. The only decent fix Ive seen so far is to replace the foam and glue everything back together.
Ive had to do this on a few resale trucks.
Replacement headliners are available but so far ever time Ive been able to salvage the base and the covering material and just purchase new foam and spray on glue to do the repair.
A material roller is also hrlpful here.

i have some 3M Spray adhesive should i use that lightly on the affected areas and hope for the best? I strongly felt like it was the wind tearing away at it, but the foam could be deteriorating from underneath, as you say

I would like to say thanks for the answers. I never thought about the foam being part of the problem. I have a shazbatload of sewing pins holding mine up. At least whats left of it after my autistic son realized how much fun it was to pull on.

The problem is if you dont get the foam out it continues to deteriorate and causes the headliner to continue to fall in other areas.
Ive seen more than one held up by pins staples tacks you wouldnt believe the methods people use to hold them up.
You could probably do without replacing the foam but make sure you clean the remnants out before glueing it..