There are important lessons to be learned from the Haneef fiasco. And they need to be learned quickly. Australia cannot wait until the next terror threat. We must get it right now.

There are three fundamental elements of the legal aspects of national security: legislation, investigation and prosecution. The Haneef case has focussed attention on the operation of all three – and, in particular, the failure of these parts to function together.

The anti-terror detention laws worked well – Haneef’s 14-day detention was fully supervised by a magistrate and thoroughly reported by the media. (For the purposes of this legal analysis, I put to one side the political debate as to whether we should have these laws at all).

The Australian Federal Police (AFP) investigated the matter and provided a (written) brief to the Commonwealth DPP (CDPP).

Any decision to prosecute is the sole responsibility of the CDPP and it has published policy guidelines. In particular, the CDPP had to be satisfied that there “was reasonable prospect of obtaining a conviction”. It is becoming increasingly likely that there was never a case with which to prosecute Haneef, and that the CDPP made a grievous error in laying the charge.

Things then got worse. The magistrate granted bail, partly because of the weak prosecution case. The Commonwealth prosecutor falsely told the court that the SIM card was recovered from the Glasgow vehicle and is reported as telling the court that it had been intended the SIM card would be destroyed in the planned explosion when a Jeep Cherokee was rammed into the doors of at Glasgow Airport.

None of the Federal police officers in court corrected these false allegations. The question then arises: were these allegations contained in the police brief?

Immediately after bail was granted, Federal Immigration Minister Andrews revoked Haneef’s work visa on character grounds. This decision was apparently based on two sets of facts: first, the criminal prosecution evidence; second, secret national security information. Now that the evidence has collapsed, the Minister must review this again.

None of this is the Minister’s fault. He is obliged to make the best decision he can based upon the information provided. It seems clear that, even though there is now no evidence of Haneef committing a crime in Australia, the fact of his close association with a terrorist group (the UK doctors) is a proper ground to revoke the visa.

The lessons to be drawn are these.

One, the AFP has so badly bungled this case that we must consider whether they are capable of handling terrorist investigations at all. I think they are not. We need an FBI-type organisation which specialises in terror and other national security investigations and which has coercive powers. Incidentally, there has been no mention of the Australian Crime Commission in the fight against terror. The AFP should have Haneef coercively interrogated by the ACC but apparently this was not done.

The CDPP is just plain incompetent. The whole organisation needs to be reviewed, and not just in relation to terror cases. In my opinion, if they had briefed an outside QC, none of this mess would have occurred. They need to be reformed, and quickly.

The immigration visa laws need to be brought in line with the terror laws. The Minister should have a complete discretion to revoke visas or refuse entry on national security grounds rather than “character”.

Australia is lucky that Haneef, whatever happens next, is a small fish. If he had been a major international terrorist he could have walked free due to our incompetent system.

In hindsight what should have happened is this: Haneef detained, a QC advises there is no case, Haneef not charged but immediately deported.

All of this has been very unfair to Haneef and he should be substantially compensated.