A Criminal Defense Lawyer can do alot, even if you are guilty. Just because you think you may be guilty of the charges, a criminal defense lawyer can review the case and defenses to see whether or not you can be proved guilty beyond a reasonable doubt. Even if you can be proved guilty, the lawyer knows what factors are important in trying to negotiate the charges with the District Attorney to get them reduced or dismissed. There may be alternatives to prosecution and different ways the case can be handled.
Mental competency is defined in California Penal Code 1367(a) which generally states that if the defendant because of a "mental disorder or developmental disability" is unable to understand the nature of the criminal procedings or assist his/her attorney in their defense in a rational manner, criminal charges will be suspended pending a return to competency. There are proceedures and hearing rights also outlined in related sections of the Penal Code.
There are alternatives in Vista Criminal cases to actual custody if custody is part of the sentence, probation was granted and there is no state prison commit. Those alternatives vary depending on the type of case, facts of the case, defendants unique situation and qualification for the various custody programs. These alternatives must be approved by the Judge doing the sentencing before they can be given any consideration. Often the type of custody, if required, is discussed and negotiated as part of the plea bargain.