
## Gap Analysis: Kuwait Prepaid Card Program vs. Current Repository

### Functional Requirements

| Req | Description | Current Repo State | Gap | Priority |
|-----|-------------|-------------------|-----|----------|
| **F01** | Card issuance & allocation | `wallet_cards`: `request_virtual_card.ex`, `card_store.ex`, admin card search UI exists in `wallet_web` | No bulk issuance from CSV; no physical card provisioning; no `apps/wallet_prepaid` app | **P0** |
| **F02** | Top-up via transferred funds | `wallet_ledger/apply_credit.ex`, `wallet_transfers` with inbound routing exist | No `TopUpCard` command that directly links inbound credit → card balance | **P0** |
| **F03** | Integrated payment & disbursement | Full ledger + settlement + transfer pipeline present across `wallet_ledger`, `wallet_transfers`, `wallet_settlement` | Config for closed-loop disbursement only; no program-scoped disbursement flow | **P1** |
| **F04** | Monthly batch top-up per statement | `wallet_wps/batch_post_salary_credits.ex` + `wps_parser.ex` exist as reusable pattern | No `BatchTopUpFromStatement` command; no `StatementParser`; WPS pattern not adapted for card top-up | **P0** |
| **F05** | 5,000–6,000 card volume | ETS + MySQL backend in `wallet_cards`; 69+ migrations show mature schema | Load test for prepaid volume not done; no `prepaid_programs` or `is_prepaid` flag on cards table | **P1** |
| **F06** | Real-time card activity reports | `wallet_reporting`: `generate_report.ex`, `schedule_report.ex`, `request_report.ex` exist; admin LiveViews present | No aggregated card portfolio report across all 5–6k cards; no per-program real-time dashboard | **P0** |
| **F07** | Cards topped up within 2 days of statement | No SLA mechanism found anywhere in codebase | `prepaid_top_up_batches` table + `deadline_at` + 48h SLA enforcement + auto-escalation entirely absent | **P0** |
| **F08** | Remaining balances settled/refunded | `wallet_disputes` refund pipeline exists | No `SweepProgramBalances` command; no bulk-zero-to-sponsor-account flow | **P0** |
| **F09** | Purchase only from designated outlets | `wallet_merchant/authorize_pos_payment.ex` exists | **No closed-loop allowlist**: no `prepaid_program_merchants` table, no `ClosedLoopPolicy` check at authorisation time | **P0** |
| **F10** | Cards issued within 2 weeks of receipt | `request_virtual_card.ex` (single card) exists | No bulk CSV issuance; no `prepaid_issuance_batches` table; no 14-day SLA tracker | **P0** |
| **F11** | Cardholder: Arabic name, English name, Phone, National ID, Amount | `display_name` (single field) only in user schema; National ID only as a KYC document type label in UI settings | No `name_en`, `name_ar`, `national_id` (encrypted) columns on `users` table; no migration | **P0** |
| **F12** | Cardholder can change PIN | `wallet_auth/pin/commands/change_pin.ex` **EXISTS** ✅ | No customer-facing self-service PIN change LiveView in `wallet_web`; UI flow missing | **P1** |
| **F13** | Monitor disbursement & account statements | `wallet_web` admin LiveViews for transactions, audit log, statements confirmed | Role-gated access already in place — **no gap** | ✅ Ready |
| **F14** | Transactions linked to name + purchase location | Ledger references `user_id`; `wallet_merchant` stores location in POS txn | No denormalised report joining cardholder `name_ar`/`name_en` + merchant geo in one export | **P1** |
| **F15** | Withdrawals limited to Kuwaiti cooperatives only | `wallet_merchant/authorize_pos_payment.ex` pipeline exists | No merchant allowlist enforcement at auth time; no `PrepaidProgramMerchants` policy check | **P0** |
| **F16** | Monthly balance report before next transfer | `wallet_reporting` engines exist; per-card balance queryable via `get_sub_wallet_balance.ex` | No programme-wide balance report query across all 5–6k cards with `name_en/ar`, `national_id`, phone | **P0** |

---

### Non-Functional Requirements

| Req | Description | Current Repo State | Gap | Priority |
|-----|-------------|-------------------|-----|----------|
| **NF01** | On-premise hosting | Standard Elixir/Phoenix — no cloud lock-in | No Docker Compose / systemd manifests; no Nginx config; no DB backup scripts | **P1** |
| **NF02** | OPEX pricing model | N/A — commercial decision | No codebase action required | N/A |
| **NF03** | Arabic language support | Gettext configured (`DaProductAppWeb.Gettext`) | No `priv/gettext/ar/` locale files; no RTL CSS (`dir="rtl"`); no `name_ar` fields anywhere | **P1** |
| **NF04** | Data security (National ID, PIN, PII) | PIN encrypted via bcrypt; `WalletDatabase.Types.EncryptedDeterministic` type available; `DataGovernance` module present | `national_id` field not yet created; must use `EncryptedDeterministic` type when added; no encryption-at-rest validation for new fields | **P0** |

---

### Summary Scorecard

| Domain | Status | Action Needed |
|--------|--------|---------------|
| Core payment & ledger infrastructure | ✅ Ready | None |
| Card lifecycle (single card) | ✅ Ready | None |
| Admin monitoring & audit trail | ✅ Ready | None |
| KYC / AML / compliance pipeline | ✅ Ready | None |
| WPS batch pattern (reusable) | ✅ Ready | Adapt for card top-up |
| PIN change command | ✅ Command exists | Add customer LiveView |
| POS authorisation hook point | ✅ Hook exists | Add `ClosedLoopPolicy` check |
| `apps/wallet_prepaid` | ❌ Missing | Create entire new OTP app |
| `name_en`, `name_ar`, `national_id` on users | ❌ Missing | DB migration + schema update |
| `is_prepaid`, `program_id` on cards | ❌ Missing | DB migration + schema update |
| Bulk CSV card issuance + 14-day SLA | ❌ Missing | `BulkIssueCards` command + issuance batches table |
| Statement-driven batch top-up + 2-day SLA | ❌ Missing | `BatchTopUpFromStatement` + top_up batches table |
| Closed-loop merchant allowlist | ❌ Missing | `PrepaidProgramMerchants` table + `ClosedLoopPolicy` |
| Programme-wide balance sweep | ❌ Missing | `SweepProgramBalances` command |
| Monthly programme balance report | ❌ Missing | New report query joining cards + cardholders |
| Arabic / RTL UI | ❌ Missing | `gettext/ar/` locale + RTL CSS |
| On-premise deployment packaging | ❌ Missing | Docker Compose + Nginx + backup scripts |

**Coverage: ~40% ready today. 8 P0 gaps, 5 P1 gaps require new code.** The proposal's `apps/wallet_prepaid` umbrella app is the single largest missing piece.