%% NOTE: The following diagram describes what happens when user 
%%       requests a NextJS page. It assumes that only one TRPC method gets 
%%       called on the page for simplicty.

sequenceDiagram
    participant Browser
    participant NextJS as NextJS Server
    participant API as NestJS API Server
    participant DB as Database
    participant Keycloak

    %% trpc.viewer
    autonumber
    
    Browser->>NextJS: GET /

    %% SSR
    
    Note left of Browser: SSR

    NextJS->>API: GET /trpc.method
    
    
    API->>Keycloak: GET /userinfo
    Keycloak-->>API: UserInfo (sub)
    API-->>DB: GET /user/(sub)
    DB-->>API: User
    API-->>API: Perform Server Method
    API-->>NextJS: Return Data
    NextJS-->>NextJS: Prerender Page
    NextJS-->>Browser: Page

    %% Client
    Browser->>API: GET /trpc.method
    Note left of Browser: Client Request

    API->>Keycloak: GET /userinfo
    Keycloak-->>API: UserInfo (sub)
    API->>DB: GET /user/(sub)
    DB-->>API: User
    API-->>API: Perform Server Method
    API-->>Browser: Return Data
