# IBT Trainee Duplicate-Check Tool — Functional Spec

## Purpose
Take a messy roster (name + phone, no stable ID) from a training provider like IBT,
clean it, find duplicates **within the roster itself**, then check every remaining
person against the existing system database across **all** providers, and report
back exactly who is a duplicate and of whom.

## Input
- CSV upload with at minimum: `first_name`, `father_name`, `phone`, `email`.
  (Optional but useful if present: `gender`, `department`, `source_sheet`.)
- Assume phone may arrive in inconsistent formats (with/without leading 0,
  with/without `251` country code, trailing `.0` from Excel, blank).
- Assume `email` and `phone` can each independently be blank. A small number of
  rows (in this IBT batch: 9 of 489) will have **both** missing — these can only
  ever be matched by name, so flag them as lowest-confidence from the start.

## Step 1 — Normalize on upload
For every row:
- `phone_clean`:
  1. Strip a trailing `.0` if present.
  2. Strip all non-digit characters.
  3. If it starts with `251` and is 12 digits, replace `251` with a single leading `0`.
  4. If it's 9 digits with no leading `0`, add one.
  5. If it's 9 digits and the system's canonical stored format is 9-digit-no-leading-zero,
     strip the leading `0` instead — **pick one canonical format up front and normalize
     everything (both the upload and the existing DB lookup) to it.**
  6. If none of the above yields a 9 or 10-digit number, mark `phone_valid = false`
     and keep the raw value for display.
- `name_key`: `lower(trim(first_name)) + " " + lower(trim(father_name))`, collapsing
  extra whitespace. (Grandfather name too, if you have it and want a stricter key.)
- `email_key`: `lower(trim(email))`, or null if blank. Treat email as a **secondary**
  match key, one notch more reliable than name alone but less reliable than phone
  (people mistype emails, share family emails, etc.).
- Keep a `row_ref` (sheet name + row number, or CSV row number) on every record so
  every result can be traced back to source.
- Compute a `data_completeness` flag per row: `has_phone`, `has_email`, both,
  or **neither** — the last group (9 of 489 in this IBT batch) can only ever be
  matched on name and should be clearly marked as needing manual review no
  matter what the matching step returns.

## Step 2 — Internal duplicate check (within the uploaded batch)
Before ever touching the system database, group the *cleaned* upload:
- **Phone-match group**: any `phone_clean` shared by 2+ rows → internal duplicate,
  high confidence.
- **Email-match group**: any `email_key` shared by 2+ rows → internal duplicate,
  medium confidence.
- **Name-match group**: any `name_key` shared by 2+ rows → internal duplicate,
  lower confidence (flag as "same name — verify same person" rather than
  auto-merging, since two trainees can genuinely share a name).
- Rows with no valid phone fall back to email-match, then name-match — flag these
  as `unverifiable_by_phone` (or `unverifiable_by_phone_and_email` for the 9 rows
  missing both) so a human knows a name-only match isn't as trustworthy.
- Output of this step: a list of internal duplicate clusters, each showing every
  row in the cluster and which match type (phone/name) linked them.

## Step 3 — Cross-check against existing system records
For every **unique** person surviving Step 2 (i.e. one row per internal cluster):
- Look up `phone_clean` against the system's existing `phonePrimary` field,
  across **all providers**, not just IBT. This is the highest-confidence check.
- For rows with no phone (or where the phone lookup found nothing), fall back to
  `email_key` against the system's existing email field, across all providers —
  medium confidence.
- For rows with neither phone nor email (or where both lookups found nothing),
  fall back to a name-match pass against the system — lowest confidence, and
  should always be surfaced for manual review rather than auto-flagged as a
  confirmed duplicate.
- On any match, pull back: existing trainee name, existing provider name, existing
  record ID (e.g. `PBPS-AAU-000475`), and **which key matched** (phone/email/name)
  so the reviewer knows how much to trust it.

## Output format
One row per person in the uploaded batch, e.g.:

| Uploaded Name | Phone | Email | Status | Matched Against | Matched Provider | Matched Record ID | Match Type |
|---|---|---|---|---|---|---|---|
| Mihret Seifu | 937349834 | m.seifu@gmail.com | Internal duplicate (3x: SQ2, SQ3, SQ4) | — | — | — | phone |
| Niguse Geda | 988379540 | — | Duplicate in system | Niguse Geda | Addis Ababa University | PBPS-AAU-000475 | phone |
| Feben Adane | 927630868 | — | No match | — | — | — | — |
| (row, phone blank) | — | j.doe@gmail.com | Duplicate in system (email match) | John Doe | Selam Children's Village | PBPS-SCV-000xxx | email |
| (row, phone + email blank) | — | — | Unverifiable — no phone or email | — | — | — | name only |

Group the report by `Status` so the person doing the review can triage, in this
priority order:
1. Internal duplicates to resolve first (pick one canonical row per person).
2. System duplicates found by phone (highest confidence — decide whether
   to skip import or link/update).
3. System duplicates found by email only (medium confidence — worth a quick look).
4. Unverifiable rows — no phone, or no phone *and* no email — needs manual lookup.
5. Clean, no-match rows (safe to import as new).

## Suggested tech notes for Cursor
- Do the phone normalization as a single pure function so it's used identically
  on upload data and on the existing DB query — the whole system breaks if the
  two sides use different canonical formats.
- Internal dedup is just a groupby on `phone_clean` and separately on `name_key`
  over the uploaded batch — no DB call needed for that part.
- The cross-check against the system is the expensive part — batch the lookup
  (single query with an `IN (...)` on all cleaned phones) rather than one query
  per row.
- Persist `row_ref` through every step so the final report can say exactly which
  original CSV/sheet row each result came from — this is what makes the report
  actually actionable instead of just a list of names.
