# Implementation Plan: Review Queue, Shortlist Promotion & Evidence State Management

## Goal Description
Implement an interactive review queue and human-in-the-loop shortlist promotion workflow within the existing Lead Generation (LEAD-GEN) UI. The goal is to provide human operators with full control over lead evaluation, evidence verification (`UNVERIFIED`, `SUPPORTED`, `VERIFIED`), and persistent qualification notes, while establishing an absolute safety gate ensuring **zero automated outreach** occurs.

---

## User Review Required

> [!IMPORTANT]
> **Outreach Guardrail**: Shortlist promotion acts purely as an internal qualification gate. No automated outreach, webhook trigger, or external communication pipeline will be connected or executed upon shortlist promotion.

> [!NOTE]
> **Evidence Verification Threshold**: We will establish whether shortlist promotion requires all evidence items to reach at least `SUPPORTED` / `VERIFIED`, or if human reviewers can promote with overriding notes. By default, promotion requires at least one `SUPPORTED` or `VERIFIED` primary signal, or an explicit reviewer sign-off note.

---

## Architecture & Data Flow

```mermaid
flowchart TD
    subgraph Candidate Ingestion
        A[Discovered Leads / Raw Signals] --> B[Default Evidence State: UNVERIFIED]
    end

    subgraph Review Queue UI
        B --> C[Review Queue View]
        C --> D[Filter by Evidence State & Status]
        C --> E[Inspect Evidence Provenance: Source & Timestamp]
        E --> F[Update Evidence: UNVERIFIED / SUPPORTED / VERIFIED]
        C --> G[Add / Edit Persistent Notes]
    end

    subgraph Human Approval Gate
        F & G --> H{Human Decision}
        H -->|Reject / Archive| I[Status: REJECTED]
        H -->|Request More Info| J[Status: NEEDS_EVIDENCE]
        H -->|Approve & Promote| K[Status: SHORTLISTED]
    end

    subgraph Safety Boundary
        K --> L[Shortlist View: Ready for Manual Evaluation]
        L -.->|STRICT BARRIER: No Autonomous Outreach| M[Automated Outreach: BLOCKED / DISABLED]
    end
```

---

## Proposed Changes

### 1. Data Schema & Model Layer

#### `Evidence & Signal State Definition`
- Define the canonical evidence verification enum:
  - `UNVERIFIED`: Signal detected from source but uninspected or pending human confirmation.
  - `SUPPORTED`: Secondary corroboration present (e.g., job postings, financial reports, press mentions).
  - `VERIFIED`: Provenance validated directly against official primary sources.
- Extend Lead & Evidence models:
  - `evidence_items`: Array of items containing `id`, `claim`, `source_url`, `source_date`, `verification_state` (`UNVERIFIED` | `SUPPORTED` | `VERIFIED`), `verified_by`, `verified_at`.
  - `review_status`: `PENDING_REVIEW`, `NEEDS_EVIDENCE`, `REJECTED`, `SHORTLISTED`.
  - `review_notes`: Array of note objects containing `id`, `author`, `created_at`, `updated_at`, `content`, `category` (e.g., `qualification_rationale`, `risk_flag`, `outreach_guidance`).
  - `promotion_metadata`: `approved_by`, `approved_at`, `gate_checklist_satisfied` (boolean).

---

### 2. Backend & State Persistence Layer

#### `Lead Review & Transition Controller`
- Implement endpoints/controllers for review transitions:
  - `PATCH /api/leads/:id/evidence/:evidenceId`: Update evidence status between `UNVERIFIED`, `SUPPORTED`, and `VERIFIED`.
  - `POST /api/leads/:id/notes`: Append persistent review note.
  - `POST /api/leads/:id/promote`: Explicit promotion gate action.
    - Validates reviewer identity and required sign-off fields.
    - Updates lead status to `SHORTLISTED`.
    - Guarantees no side-effect dispatcher or outreach worker is invoked.
  - `POST /api/leads/:id/reject`: Transition lead to `REJECTED` with required rejection rationale note.

---

### 3. UI Layer (Review Queue, Details, & Filters)

#### `Review Queue Dashboard & Filter Toolbar`
- **Filter Controls**:
  - **Queue Status**: `All`, `Pending Review`, `Shortlisted`, `Rejected`.
  - **Evidence Status**: Filter leads containing `UNVERIFIED`, `SUPPORTED`, or `VERIFIED` evidence items.
  - **Search & Campaign**: Search by company name, target role, keyword, or campaign.
  - **Sorting**: By creation date, signal strength, or last review activity.

#### `Lead Card & Evidence Inspector Panel`
- **Evidence Badge Components**:
  - Color-coded badges:
    - `UNVERIFIED`: Neutral / Amber badge with indicator.
    - `SUPPORTED`: Blue / Information badge.
    - `VERIFIED`: Green / Success badge.
  - Quick-toggle dropdown or segmented button on each evidence item to transition states.
  - Clickable source provenance link and extraction date.

#### `Persistent Review Notes Section`
- Threaded/chronological notes display with timestamp and reviewer identity.
- Rich/plain text input with quick markdown formatting and persistent autosave/save action.

#### `Human Approval Gate Modal / Action Bar`
- Prominent action buttons: **Promote to Shortlist**, **Request More Evidence**, **Reject**.
- On clicking **Promote to Shortlist**:
  - Displays pre-flight confirmation dialog showing:
    - Summary of evidence verification counts (`X Verified`, `Y Supported`, `Z Unverified`).
    - Explicit disclaimer: *"Promotion adds prospect to verified shortlist only. No automated contact will be initiated."*
    - Mandatory confirmation button: `Confirm Shortlist Promotion`.

---

## Verification Plan

### Automated Tests
1. **Model & State Validation Tests**:
   - Verify state transition machine: leads cannot transition to `SHORTLISTED` without human approval metadata.
   - Verify evidence state updates accurately persist and reflect across parent lead aggregates.
   - Verify persistent notes serialize, store, and order correctly by timestamp.
2. **Safety & Zero-Outreach Guarantee Test**:
   - Unit and integration test verifying `promote_to_shortlist` triggers zero outbound HTTP requests, email jobs, queue messages, or messaging workers.

### UI Smoke Tests (Playwright / Cypress / E2E)
1. **Queue Rendering & Filtering**:
   - Load review queue with mock pilot campaign leads.
   - Apply filter `Evidence State = UNVERIFIED`; verify filtered list matches criteria.
   - Apply filter `Status = SHORTLISTED`; verify non-shortlisted leads are excluded.
2. **Evidence State Modification**:
   - Select a lead; change an evidence item from `UNVERIFIED` to `SUPPORTED` and another to `VERIFIED`.
   - Reload page; verify updated evidence states persist.
3. **Persistent Notes**:
   - Add a qualification review note (e.g., *"Confirmed Oracle estate renewal in Q4 2026 via annual filing"*).
   - Reload page; confirm note appears with correct author and timestamp.
4. **Approval Gate & Shortlist Promotion**:
   - Open pending lead, click `Promote to Shortlist`, confirm modal.
   - Verify lead status changes to `SHORTLISTED` and appears in Shortlist tab.
   - Confirm network tab shows no external contact or outreach service calls.