# `AshBilling.Diagnostics`
[🔗](https://github.com/sfelixalejandro/ash_billing/blob/v0.7.0/lib/ash_billing/diagnostics.ex#L1)

Layered diagnostics for verifying `AshBilling` structural contracts, PostgreSQL
schema readiness, and optional read-only provider connectivity.

Diagnostics are divided into three independent layers:

1. `:structural` — Pure static checks on Ash resources, attributes, relationships,
   calculations, identities, write-protection, private exposure of `WebhookEvent`,
   single-Repo compatibility, multitenancy alignment, deduplication strategy,
   credential prefixes (without printing secrets), and HTTP timeout configuration.
2. `:database` (opt-in via `check_database?: true`) — Read-only PostgreSQL
   connectivity, required tables and columns (including `0.6.0` `last_reconciled_at`
   and `0.7.0` `quantity` / `provider_subscription_item_id`), critical unique indexes,
   and optional non-negative quantity check constraint.
3. `:provider` (opt-in via `check_provider?: true`) — Optional read-only provider
   probe (Stripe `GET /v1/balance` by default) confirming reachability and livemode
   alignment without exposing credentials.

`configuration_valid` and `production_ready` are deliberately separate booleans:
a configuration with no failures (`configuration_valid: true`) is **not** marked
`production_ready: true` if warnings remain (such as in-memory deduplication,
unbounded HTTP retry/timeout defaults, unverified database schema, or missing
`expected_livemode`).

# `check`

```elixir
@type check() :: %{
  id: atom(),
  layer: layer(),
  status: status(),
  message: String.t(),
  remediation: String.t() | nil
}
```

# `layer`

```elixir
@type layer() :: :structural | :database | :provider
```

# `report`

```elixir
@type report() :: %{
  status: status(),
  configuration_valid: boolean(),
  production_ready: boolean(),
  summary: %{
    pass: non_neg_integer(),
    warn: non_neg_integer(),
    fail: non_neg_integer()
  },
  layers: %{structural: [check()], database: [check()], provider: [check()]},
  checks: [check()]
}
```

# `status`

```elixir
@type status() :: :pass | :warn | :fail
```

# `format_text`

```elixir
@spec format_text(report()) :: String.t()
```

Formats a diagnostics report as human-readable text with `PASS`, `WARN`, and `FAIL` statuses.

# `run`

```elixir
@spec run(keyword()) :: report()
```

Runs diagnostics and returns a structured, JSON-serializable report.

## Options

  * `:customer_resource` - Ash customer resource module.
  * `:subscription_resource` - Ash subscription resource module.
  * `:deduplication_resource` / `:webhook_event_resource` - Ash webhook event resource module.
  * `:deduplication_strategy` - `:resource` or `:memory` (defaults to application config or `:resource` when `:deduplication_resource` is provided).
  * `:api_key` - Provider API key (format checked only; never echoed).
  * `:webhook_secret` - Webhook signing secret (format checked only; never echoed).
  * `:expected_livemode` - Optional boolean (`true` / `false`) for Stripe webhook mode binding.
  * `:stripe_account` - Optional Connect account ID.
  * `:req_opts` / `:req_options` - Outbound HTTP `Req` options to validate bounded timeouts and retry settings.
  * `:check_database?` - When `true`, executes read-only PostgreSQL connectivity, column, and index checks. Defaults to `false`.
  * `:check_provider?` - When `true`, executes a read-only provider connectivity/livemode probe. Defaults to `false`.
  * `:provider_check_fun` - Optional 1-arity function `(opts) -> {:ok, %{livemode: boolean()}} | {:error, term()}` for custom providers or testing.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
