# The Signable behaviour

`Countersign.Signable` is the thing being signed: a contract, a consent
form, a plan of care. Countersign never stores it. A request holds only
`subject_type` and `subject_id`, and asks your module for everything else.
One module can serve several kinds of subject by matching on
`subject_type`.

## Fetching, in scope

`c:Countersign.Signable.fetch/3` loads the subject for a scope. Answer
`{:error, :not_found}` for anything outside that scope: this is the tenant
boundary, and a request of one scope must never reach another scope's
record. Whatever you answer with `{:ok, subject}` is passed back untouched to
the other callbacks.

`c:Countersign.Signable.signable?/1` says whether the subject still takes
signatures (not voided, not already complete). A send for an unsignable
subject is refused with `{:error, :not_signable}`.

## The content hash

`c:Countersign.Signable.content_hash/1` answers 64 lowercase hex characters,
a SHA-256 of the content the signer reads. A request is sent against this
hash; at signing, Countersign fetches the subject again under your subject
lock, and if the hash moved it records no signature and marks the request
`superseded` (the link is dead; send again for the new content). So the
hash must cover
exactly what the signer agrees to: the document's text and the fields it
shows, and not an `updated_at` or a view counter that changes when nothing
the signer reads did.

When your app changes a subject that has open requests, call
`Countersign.Requests.supersede/4` with the new hash and mode `:stale`,
inside the transaction that changed it and after your subject lock, so
those links stop offering the old content and a racing send sees either
the old content or the new one.

## Rendering

`c:Countersign.Signable.render_pdf/2` renders the subject as a PDF for
`:review` (what the signer reads before signing) or `:copy` (their signed
copy). Answer `{:error, :busy}` when your renderer is saturated; the page
asks the signer to try again.

`c:Countersign.Signable.type_label/1` and
`c:Countersign.Signable.role_label/2` are the human names the pages and the
certificate show: "Plan of Care", "Client", "Responsible party".

## Recording the signature

`c:Countersign.Signable.record_signature/4` runs inside the sign
transaction, after the lock and the hash check. Write the signature into
your own records (a signatures table, a column on the subject) and answer
`{:ok, signature_ref}`; Countersign stores the ref on the request.

The input carries `:signer_name`, `:method` (`:drawn` or `:typed`) and, for
a drawn signature, `:image` (PNG bytes). The meta carries `:ip_address` and
`:user_agent`.

Answer `{:error, :already_signed}` when the role's line is already signed
(by paper, or by another channel), and `{:error, :changed}` if your own
check finds the content moved. Either answer records no signature and
marks the request `superseded`, which commits and notifies `:supersede`:
the link is dead, and the sender sends again if a signature is still
wanted. Any other error rolls the whole transaction back, and the request
stays pending.

Make the write conditional, so two signatures racing for one line cannot
both land:

```elixir
@impl true
def record_signature(%Agreement{id: id}, _request, input, meta) do
  {count, _} =
    Repo.update_all(
      from(a in Agreement, where: a.id == ^id and is_nil(a.signed_at)),
      set: [
        signed_by_name: input.signer_name,
        signed_method: to_string(input.method),
        signed_at: DateTime.utc_now(),
        signed_ip: meta[:ip_address]
      ]
    )

  case count do
    1 -> {:ok, "agreement:#{id}"}
    0 -> {:error, :already_signed}
  end
end
```

## Storing the certificate

`c:Countersign.Signable.store_document/4` keeps the signing certificate PDF
against the subject (your documents table, object storage) and answers
`{:ok, document_ref}`, which is pinned on the request as `certificate_ref`.
The certificate job calls it with the request row locked, so a rerun after
a crash finds the ref pinned and stores nothing twice.

Answer `{:error, :busy}` or `{:error, :timeout}` to snooze the job,
`{:cancel, reason}` when the subject is gone for good, and any other error
to retry.
