sequenceDiagram
    participant C as React Client
    participant WS as WebSocket Server
    participant AUTH as Auth Service
    participant DB as PostgreSQL
    participant AS as Analysis Service
    participant A1 as Legal Agent
    participant A2 as Brand Agent
    participant A3 as Channel BP Agent
    participant A4 as Channel TS Agent
    participant LA as Lead Agent
    participant GEM as Gemini API

    C->>WS: Connect /ws/analyze
    WS-->>C: Connection established

    C->>WS: {"type":"analyze", "file_data":"base64...", "access_token":"jwt..."}

    WS->>AUTH: verify_access_token(jwt)
    AUTH-->>WS: claims {oid, name, role}

    WS->>DB: Fetch previous version analysis
    DB-->>WS: previous_analysis (if revision)

    par Parallel Agent Execution
        WS-->>C: {"type":"agent_started", "agent_name":"Legal Agent"}
        AS->>A1: analyze(images, prev_review)
        A1->>GEM: Gemini API call
        GEM-->>A1: Response
        A1-->>AS: SubReview
        WS-->>C: {"type":"agent_completed", "agent_name":"Legal Agent", "review":{...}}
    and
        WS-->>C: {"type":"agent_started", "agent_name":"Brand Agent"}
        AS->>A2: analyze(images, prev_review, brand)
        A2->>GEM: Gemini API call
        GEM-->>A2: Response
        A2-->>AS: SubReview
        WS-->>C: {"type":"agent_completed", "agent_name":"Brand Agent", "review":{...}}
    and
        WS-->>C: {"type":"agent_started", "agent_name":"Channel BP Agent"}
        AS->>A3: analyze(images, prev_review)
        A3->>GEM: Gemini API call
        GEM-->>A3: Response
        A3-->>AS: SubReview
        WS-->>C: {"type":"agent_completed", "agent_name":"Channel BP Agent", "review":{...}}
    and
        WS-->>C: {"type":"agent_started", "agent_name":"Channel TS Agent"}
        AS->>A4: analyze(images, prev_review)
        A4->>GEM: Gemini API call
        GEM-->>A4: Response
        A4-->>AS: SubReview
        WS-->>C: {"type":"agent_completed", "agent_name":"Channel TS Agent", "review":{...}}
    end

    AS->>LA: synthesize(all_reviews)
    LA->>GEM: Gemini API call
    GEM-->>LA: Summary + RAG status
    LA-->>AS: overall_status, summary

    AS->>DB: Persist proof + version + agent_review
    AS->>DB: Store file to disk

    WS-->>C: {"type":"complete", "result":{...}, "proof_id":"uuid", "version_id":"uuid"}
