People accused of crimes are presumed to be of sound mind, unless proven otherwise. If somebody is not of sound mind at the relevant time, it would be unfair to hold them criminally responsible for their actions.
Until relatively recently, an accused person would have to rely on a common law plea of “insanity” as a special defence. Insanity (as far as I know) is a legal term, not a medical one. Here are two passages that summarise the principles underpinning insanity and criminal responsibility:
Brennan v HMA (1977 JC 38)
“[I]nsanity in our law requires proof of total alienation of reason in relation to the act charged as the result of mental illness, mental disease or unsoundness of mind.”
Cardle v Mulraney (1992 SLT 1152)
“Where … the accused knew what he was doing and was aware of the nature and quality of his acts and that what he was doing was wrong, he cannot be said to be suffering from the total alienation of reason in regard to the crime with which he is charged which the defence requires … [An] inability to exert self-control … must be distinguished from the essential requirement that there should be a total alienation of the accused’s mental faculties of reasoning and of understanding what he is doing“.
Section 171 of the Criminal Justice and Licensing (Scotland) Act 2010 abolished insanity as a special defence. That said, section 168 simultaneously introduced its replacement, by amending the Criminal Procedure (Scotland) Act 1995 to introduce section 51A, which provides for a special defence of mental disorder.
“Mental disorder” is defined by s328 of the Mental Health (Care and Treatment) (Scotland) Act 2003:
So, the defence has to prove, on the balance of probabilities (more likely than not) that the accused was, by reason of mental disorder (which cannot be a personality disorder that primarily manifests itself in “unusually aggressive or seriously irresponsible conduct”), unable to appreciate the nature or wrongfulness of the conduct. If they do that, then the accused has to be acquitted.
It doesn’t matter whether the accused suffered from the mental disorder temporarily or permanently, provided it was present at the time of the offence.
In practice, you would expect to lead expert medical evidence as to your client’s condition at the relevant time. The Crown may counter your expert evidence with its own expert(s). In fact, it is quite possible for the actual facts of the charge to be incidental to the trial (both parties may formally “agree” in a Joint Minute that the act happened, and that the accused carried it out), and the only area of disagreement is the accused’s mental state at the time.
Ultimately, it is a question of fact for the jury (or JP/Sheriff, depending on whether it is a jury trial or not) to decide.
If it is found that the accused committed the actus reus in question, but the special defence has been proved, then the verdict has to reflect that. Section 53E of the 1995 Act applies.
Acquittal by reason of mental disorder does not necessarily mean that there will be no consequences for the accused. Section 57 of the 1995 Act gives the court power to make various orders in this scenario, which can include detaining the acquitted person in hospital. Here’s an example:

It is also worth noting the concept of unfitness for trial, which is covered by s53F of the 1995 Act.
Insanity / mental disorder is different to the concept of diminished responsibility.