# Start Process Queue — Function Flow

Flow from **`lambda_handler`** in `start_process_queue` (and related modules).

---

## Mermaid flowchart

```mermaid
flowchart TB
    subgraph entry["start_process_queue/app.py"]
        A["lambda_handler(event, context)"]
        A --> B["get_parameter('/supabase/url')"]
        A --> C["get_parameter('/supabase/anon')"]
        B --> D["create_client(url, key)"]
        C --> D
        D --> E["date = event['week_start_date']"]
        E --> F["Phase 1: update_task_customer_ids"]
    end

    subgraph phase1["Phase 1 — Batch update task customer IDs"]
        F --> G{"while True"}
        G --> H["supabase.rpc('update_task_customer_ids', p_batch_size, p_hours_back)"]
        H --> I{"updated < BATCH_SIZE or loops >= MAX_LOOPS?"}
        I -->|No| G
        I -->|Yes| J["(continue)"]
        F -.->|on exception| K["email_service.send_notification(error)"]
        K --> J
    end

    subgraph phase2["Phase 2 — Archive old tasks"]
        J --> L["supabase.rpc('archive_old_tasks').execute()"]
        L -.->|on exception| M["email_service.send_notification(error)"]
        M --> N["(continue)"]
        L --> N
    end

    subgraph phase3["Phase 3 — Clear & repopulate queue"]
        N --> O["supabase.table('store_processing_queue').delete().neq('id', 0).execute()"]
        O --> P["supabase.rpc('populate_store_process_queue').execute()"]
        P -.->|on exception| Q["email_service.send_notification(error)"]
        Q --> R["(continue)"]
        P --> R
    end

    subgraph phase4["Phase 4 — Invoke backup Lambda"]
        R --> S["LAMBDA_CLIENT.invoke(BACKUP_SJREPORT_ARN, Payload=event, InvocationType='Event')"]
        S --> T["Phase 5: Success path"]
    end

    subgraph success["Success"]
        T --> U["email_service.send_notification(success)"]
        U --> V["return 200 + email_id"]
    end

    subgraph toplevel_error["Top-level exception"]
        A -.->|any uncaught exception| W["email_service.send_notification(error)"]
        W --> X["return 500 + email_id"]
    end

    subgraph email_module["email_service.py"]
        U --> Y["EmailService.send_notification(...)"]
        K --> Y
        M --> Y
        Q --> Y
        W --> Y
        Y --> Y1["_get_parameter (if client_email)"]
        Y --> Y2["MIMEMultipart + SMTP_SSL send"]
        Y2 --> Y3["return Message-ID / None"]
    end

    subgraph ssm["AWS / Supabase"]
        B --> SSM["SSM get_parameter"]
        C --> SSM
        H --> DB["Supabase (RPC)"]
        L --> DB
        O --> DB
        P --> DB
        S --> LAMBDA["Lambda: BackupSjreport (async)"]
    end
```

---

## Simplified linear flow (no branches)

| Step | Function / action | Module |
|------|-------------------|--------|
| 1 | `lambda_handler(event, context)` | app.py |
| 2 | `get_parameter(name)` | app.py |
| 3 | `create_client(url, key)` | supabase |
| 4 | Loop: `supabase.rpc('update_task_customer_ids', ...)` | app.py → Supabase |
| 5 | (on error) `email_service.send_notification(...)` | app.py → email_service |
| 6 | `supabase.rpc('archive_old_tasks').execute()` | app.py → Supabase |
| 7 | (on error) `email_service.send_notification(...)` | app.py → email_service |
| 8 | `supabase.table('store_processing_queue').delete().neq('id', 0).execute()` | app.py → Supabase |
| 9 | `supabase.rpc('populate_store_process_queue').execute()` | app.py → Supabase |
| 10 | (on error) `email_service.send_notification(...)` | app.py → email_service |
| 11 | `LAMBDA_CLIENT.invoke(BACKUP_SJREPORT_ARN, ...)` | app.py → AWS Lambda (BackupSjreport) |
| 12 | `email_service.send_notification(success)` | app.py → email_service |
| 13 | Return 200 + body | app.py |

**EmailService (email_service.py):**

| Function | Called by | Calls |
|----------|-----------|--------|
| `EmailService.__init__` | module load | `_initialize_config()` |
| `_initialize_config` | `__init__` | `_get_parameter` (×3) |
| `_get_parameter(name)` | `_initialize_config`, `send_notification` | SSM `get_parameter` |
| `send_notification(date, status, ...)` | app.py (multiple places) | `_get_parameter` (if client_email), SMTP send |

---

## External dependencies

- **SSM:** `get_parameter` (app.py), `_get_parameter` (email_service.py) → `/supabase/url`, `/supabase/anon`, `/gmail/*`
- **Supabase RPCs:** `update_task_customer_ids`, `archive_old_tasks`, `populate_store_process_queue`
- **Supabase table:** `store_processing_queue` (delete)
- **Lambda:** `BACKUP_SJREPORT_ARN` → **BackupSjreport** (async invoke)
