The car has 160 000 on it and basically the only way I can explain this is if I tell the whole story...
It all started when I first bought the car a year ago, my first car and I loved it to bits but I didn't know that the rain rails needed cleaning every once and a while otherwise they will get clogged and flood the car so the car flooded the passenger side floor which I then found out the ecu sits there under where a passengers feet would be ( if I am correct). After this the car would stall when I was coming up to stop signs or red lights.
I took it to a mechanic which said the ecu had water in it so the ecu got rebuilt which it was ether that or a new ecu. Shortly after that the car started stalling again when the car was warm and running for 20 mins to half an hour when I would come to a stop after driving it. The car would sometimes idle really badly when it was cold or warm and sometimes felt like it was about to stall. So after many many mechanics and searching forums I still cant solve the issue and it has gotten worse.
Two days ago I was driving it around to see if the problem was still there and it started stalling when I was coming to a stop at traffic lights. I then accelerated a bit to stop it from stalling, the lights went green and all I could think of doing is trying to get it onto the footpath to try and not block up a 3 lane road. I then got it off the clutch and into first gear and then second. While I was in second gear it was revving really erratically without me touching the gas peddle any less or more. When I say erratically I mean jumping from 2k to 4k to 5 and anywhere in between all without me pressing the gas peddle any more or less almost like the car was going to stall in gear but with the revvs jumping around. The car then stalled so I turned the car off and waited for half an hour for it to cool down on the footpath I found a driveway onto. After that it started just fine, drove for half an hour without stalling or jumping around in the rpm but when I was coming to a stop, about 45 mins of driving passes and the car stalls again ( 20 mins from home luckily) and I did the same thing, turned the car off and left it for half an hour and tried to start it again and it started.
A few things about the issue I have noticed:
1. It doesn't happen regularly and almost always seems to be when I have been driving the car for at least half an hour or more
2. Sometimes the car does not start first go when cold or warm
3. The service records don't accelerate tell me what parts where replaced or where a issue was so I don't know what to replace and what needs replacing
4. When it would stall it wouldn't start after the stall, it would just turn over and when I got out of the car and got someone to try start it to see if the spark plugs where sparking they were not sparking. After waiting till the car cooled down the car's spark plugs would start sparking again and the car would start without any hesitation.
5. The rpm jumping was while I was in gear and the car almost felt like it was trying to stall but still the rpm jumping up and down without me touching the peddle any harder or softer which I still do not understand.
I have read a lot about the idle drop issue when it drops almost to stalling point and I have got a vague idea on what could be the issues. From what I have read it could be caused by any of these parts;
1. Idle Air Control Valve
2. A dirty air filter causing dust or grime in the throttle body which could cover those idle controller thingo's
3. Fuel pump or fuel filter
4. A vacuum leak ether in the gasket seal or one of the hoses to do with the air intake
5. Spark plugs, Coils or something to do with the spark side of things.
Now I am no mechanic and I do not consider my self that knowledgeable on cars or mx 5's but after working to get this problem solved with many mechanics/garages/friends that are mechanics and don't charge me for there time

I am sorry for the essay that I have wrote but I really don't know what else to do. The problem seems unsolvable and I have no where else to go except here. I hope I have posted this in the right section as I am new on this forum so sorry to the admins if its in the wrong place. Any information or help is so so sooo appreciated as every mechanic I have shown the issue to has said they have no idea and by the way things are turning out it looks like I am going to have to replace every part in the car to figure out the problem. If I knew the car would have this much issues before I bought it I wouldn't of got it in the first place but I am stuck with it now, I have spent so much money and time on people trying to figure it out and no one has and I cant sell it while it is this dangerous. As my first car I can honestly say its turned me off driving a car ever again let alone buying a Mazda again. At the moment with the stalling thing the other day I nearly crashed because of it stalling and having no breaks at 70 so this is the cars last chance, ether these last parts get bought and hopefully it gets fixed or the car gets towed to the wreckers.
Thanks for reading and I hope someone can help.
Cheers