1. 회원가입

sequenceDiagram
    actor Client
    participant Gateway
    participant UserService
    participant Keycloak
    participant UserDB

    Client->>Gateway: POST /api/v1/users/signup
    Gateway->>UserService: 요청 전달
    UserService->>Keycloak: 유저 생성 요청 (email, password)
    alt Keycloak 등록 실패
        Keycloak-->>UserService: 실패
        UserService-->>Client: 400 Bad Request
    end
    Keycloak-->>UserService: keycloakId 반환
    UserService->>UserDB: p_users 저장 (keycloakId, email, nickname)
    alt DB 저장 실패
        UserService->>Keycloak: 유저 삭제 시도 (롤백)
        alt Keycloak 삭제도 실패
            UserService->>UserService: 에러 로그 기록 + 관리자 알림
            Note over UserService: 고아 계정 수동 정리 필요
        end
        UserService-->>Client: 500 Error
    end
    UserDB-->>UserService: 저장 완료
    UserService-->>Client: 201 Created

2. 로그인

sequenceDiagram
    actor Client
    participant Gateway
    participant Keycloak

    Client->>Gateway: POST /api/v1/auth/login (email, password)
    Gateway->>Keycloak: 로그인 요청 전달
    alt 이메일/비밀번호 불일치
        Keycloak-->>Client: 401 Unauthorized
    end
    Keycloak-->>Client: 200 OK (accessToken, refreshToken)

3. API 인증 흐름 (Gateway JWT 검증)

sequenceDiagram
    actor Client
    participant Gateway
    participant Keycloak
    participant Service

    Client->>Gateway: API 요청 (Authorization: Bearer JWT)
    alt Authorization 헤더 없음
        Gateway-->>Client: 401 Unauthorized
    end
    Gateway->>Keycloak: JWKS로 JWT 서명 검증 (Keycloak 호출 없이)
    alt JWT 유효하지 않음
        Gateway-->>Client: 401 Unauthorized
    end
    Gateway->>Service: 요청 전달 (X-User-Id, X-User-Role 헤더 주입)
    Service-->>Client: 응답 반환

4. 내 프로필 조회

sequenceDiagram
    actor Client
    participant Gateway
    participant UserService
    participant UserDB

    Client->>Gateway: GET /api/v1/users/me
    Gateway->>UserService: 요청 전달 (X-User-Id 헤더)
    UserService->>UserDB: userId로 유저 조회
    UserDB-->>UserService: 유저 정보 반환
    UserService-->>Client: 200 OK (userId, email, nickname, slackId)

5. 배송지 등록

sequenceDiagram
    actor Client
    participant Gateway
    participant UserService
    participant UserDB

    Client->>Gateway: POST /api/v1/users/addresses
    Gateway->>UserService: 요청 전달 (X-User-Id 헤더)
    UserService->>UserDB: p_user_addresses 저장
    UserDB-->>UserService: 저장 완료
    UserService-->>Client: 201 Created