What I understand so far
Easy cases: I understand we only care about odd $n$ since even $n$ is never prime unless $n=2$.
Basic setup: So if $n$ is odd, then $n-1$ is even, so we can write $n - 1 = 2^r d$ for positive $r, d$ (where $d$ is odd).
Fermat's little theorem: I understand that for some integer $a$ that isn't divisible by $p$, if $p$ is prime then $a^{p-1} \equiv 1 \pmod p$ must be true.
Euclid's lemma and modular squares: If we have $x^2 \equiv 1 \pmod p$ for some prime $p>2$, this rearranges to $(x-1)(x+1) \equiv 0 \pmod p$, meaning either $x \equiv 1 \pmod p$ is true or $x \equiv -1 \pmod p$ is true (but not both at the same time).
What I don't understand
The rest of it. Let's assume we have odd candidate $n > 2$, and let's also assume it happens to be prime, $n = p$.
Then $a^{p-1} \equiv 1 \pmod p$ can be rewritten as $a^{2^r d} \equiv 1 \pmod p$ by substituting the basic setup equation.
If I set $x^2 \equiv a^{p-1} \pmod p$, then $x \equiv \sqrt{a^{p-1}} \equiv a^{\frac{p-1}{2}} \equiv a^{\frac{2^rd}{2}} \equiv a^{2^{r-1}d} \pmod p$ as long as $r>0$.
Once $r=0$, I don't think we can go any further since $d$ is odd, but I don't know if it's still possible somehow to keep going, i.e. can I do $x \equiv \sqrt{a^{2k+1}} \pmod p$?
Anyways, I know from the Euclid / modular square step that this means $a^{2^{r-1}d} \equiv 1 \pmod p$ or $a^{2^{r-1}d} \equiv -1 \pmod p$ (but I don't know if this also needs to apply to the original $a^{2^rd}$ as well).
My questions:
Did I make any mistakes so far in any of this?
Where do we go from here? Is the idea to start off with $a^{2^{r-1}d} \bmod p$ and make sure it always equals either $1$ or $-1$ modulo $p$ every time we decrease the exponent of $r$? If it ever equals something else, $n$ isn't prime?
What do we do if we hit $-1$? Where can we go from there, since we no longer have our $x^2 \equiv 1 \pmod p$ prerequisite?
Even if we choose a random $a < p$ and it passes all tests, do we just keep picking $a$ terms until something fails? Do we have to exhaustively tests all $1 < a < p$?
What about $a = 0$? I know this violates the Fermat's little theorem assumption, but is it ever possible that we have to try it to ensure the input is prime or not somehow if everything else fails?