# High-level Database Schema — v0.2.0

## Physical tables through Phase 2

### Identity / Access
- `users`
- `sessions`
- `otp_challenges`
- `roles`
- `permissions`
- `role_user`
- `permission_role`

Phase 2 adds `national_code`, mobile verification/deactivation metadata and
actor references to `users`, plus delivery metadata to `otp_challenges`.

### SMS
- `sms_gateways`
- `sms_templates`
- `sms_logs`

Gateway secret configuration is encrypted as one opaque payload. OTP values are
not persisted in SMS logs; stored content is redacted.

### Notification
- `notification_events`
- `notification_deliveries`

An event describes the business notification. Each requested channel receives a
separate delivery row and state. This is the extension point for future Push and
Bale implementations.

### Core / Settings / Audit
- `settings`
- `audit_logs`

### Version / Updates
- framework `migrations`
- `system_versions`
- `update_packages`
- `update_logs`

### Async foundation
- `jobs`
- `job_batches`
- `failed_jobs`

## Planned later domains

### Service Catalog
- `service_categories`
- `services`
- `service_access_rules`

### Providers
- `providers`
- `provider_credentials`
- `provider_services`
- `provider_health_checks`
- `provider_request_logs`

### Pricing
- `pricing_rules`
- `pricing_rule_targets`
- `pricing_snapshots`

Resolution priority: user → organization/company → group/representative → public.

### Wallet
- `wallets`
- `wallet_ledger_entries`
- `wallet_holds`
- `wallet_idempotency_keys`

All financial values are stored as integer IRR. No direct balance mutation is
permitted outside the Wallet service / ledger transaction.

### Payments
- `payment_intents`
- `payment_attempts`
- `payment_callbacks`
- `payment_verifications`

### Inquiry Engine
- `inquiries`
- `inquiry_attempts`
- `inquiry_results`
- `inquiry_events`

Each inquiry snapshots resolved cost, sale price, pricing source, provider,
request ID and idempotency key.

### Accounting
- `chart_of_accounts`
- `journal_entries`
- `journal_lines`
- `expenses`

### Notification expansion
- `push_subscriptions`
- `bale_accounts`

### Support
- `tickets`
- `ticket_messages`
- `ticket_assignments`

## Indexing principles
- composite status + date indexes for operational lists;
- user + date indexes for user histories;
- unique mobile/national-code identity constraints;
- channel/status/date indexes for deliveries;
- gateway/status/date indexes for SMS logs;
- unique idempotency/reference constraints in financial phases;
- pagination for unbounded lists.
