๐ŸŽŸ๏ธ Coupon Service

1. ์ฟ ํฐ ์ƒ์„ฑ (๊ด€๋ฆฌ์ž)

sequenceDiagram
    actor Admin
    participant Gateway
    participant CouponService
    participant CouponDB
    participant Redis

    Admin->>Gateway: POST /api/v1/coupons
    Gateway->>CouponService: ์š”์ฒญ ์ „๋‹ฌ (X-User-Role: ROLE_ADMIN)
    CouponService->>CouponDB: p_coupons ์ €์žฅ
    CouponDB-->>CouponService: couponId ๋ฐ˜ํ™˜
    CouponService->>Redis: ์žฌ๊ณ  ์„ธํŒ… SET coupon:stock:{couponId} totalQuantity
    Redis-->>CouponService: ์™„๋ฃŒ
    CouponService-->>Admin: 201 Created

2. ์„ ์ฐฉ์ˆœ ์ฟ ํฐ ๋ฐœ๊ธ‰

sequenceDiagram
    actor Client
    participant Gateway
    participant CouponService
    participant Redis
    participant CouponDB
    participant OutboxDB
    participant Kafka

    Client->>Gateway: POST /api/v1/coupons/{couponId}/issue
    Gateway->>CouponService: ์š”์ฒญ ์ „๋‹ฌ (X-User-Id ํ—ค๋”)
    CouponService->>Redis: ์ค‘๋ณต ๋ฐœ๊ธ‰ ํ™•์ธ SISMEMBER coupon:issued:{couponId} userId
    alt ์ด๋ฏธ ๋ฐœ๊ธ‰๋จ
        Redis-->>CouponService: true
        CouponService-->>Client: 409 ์ด๋ฏธ ๋ฐœ๊ธ‰๋œ ์ฟ ํฐ
    end
    CouponService->>Redis: ์žฌ๊ณ  ์ฐจ๊ฐ DECR coupon:stock:{couponId}
    alt ์žฌ๊ณ  ์—†์Œ (0 ์ดํ•˜)
        Redis-->>CouponService: 0 ์ดํ•˜
        CouponService->>Redis: INCR ๋ณต๊ตฌ
        CouponService-->>Client: 409 ํ’ˆ์ ˆ
    end
    Redis-->>CouponService: ๋‚จ์€ ์žฌ๊ณ  ๋ฐ˜ํ™˜
    CouponService->>Redis: ๋ฐœ๊ธ‰ ๊ธฐ๋ก SADD coupon:issued:{couponId} userId
    Note over CouponService,OutboxDB: ๊ฐ™์€ ํŠธ๋žœ์žญ์…˜
    CouponService->>CouponDB: p_user_coupons ์ €์žฅ
    CouponService->>OutboxDB: p_coupon_outbox ์ €์žฅ (PENDING)
    CouponDB-->>CouponService: ์ €์žฅ ์™„๋ฃŒ
    CouponService-->>Client: 201 Created
    CouponService->>Kafka: ์•„์›ƒ๋ฐ•์Šค ์Šค์ผ€์ค„๋Ÿฌ โ†’ COUPON_ISSUED ์ด๋ฒคํŠธ ๋ฐœํ–‰

3. ์ฃผ๋ฌธ ์‹œ ์ฟ ํฐ ์‚ฌ์šฉ (INTERNAL)

sequenceDiagram
    participant OrderService
    participant CouponService
    participant CouponDB

    OrderService->>CouponService: POST /api/v1/internal/coupons/{userCouponId}/use
    CouponService->>CouponDB: ์ฟ ํฐ ์กฐํšŒ
    alt ์ฟ ํฐ ์—†์Œ
        CouponService-->>OrderService: 404 Not Found
    end
    alt ์†Œ์œ ์ž ๋ถˆ์ผ์น˜
        CouponService-->>OrderService: 403 Forbidden
    end
    alt ๋งŒ๋ฃŒ๋œ ์ฟ ํฐ (expired_at < NOW())
        CouponService-->>OrderService: 410 Gone
    end
    alt ์ด๋ฏธ ์‚ฌ์šฉ๋œ ์ฟ ํฐ
        CouponService-->>OrderService: 409 Conflict
    end
    CouponDB-->>CouponService: ์ฟ ํฐ ์ •๋ณด ๋ฐ˜ํ™˜
    CouponService->>CouponDB: status = USED ๋ณ€๊ฒฝ
    CouponDB-->>CouponService: ์™„๋ฃŒ
    CouponService-->>OrderService: 200 OK (discountType, discountValue, maxDiscountAmount, discountAmount, finalPrice)
    Note over OrderService: Order Service๊ฐ€ ํ• ์ธ ๊ธˆ์•ก ๊ณ„์‚ฐ

4. ์ฟ ํฐ ๋ณต๊ตฌ - SAGA ๋ณด์ƒ ํŠธ๋žœ์žญ์…˜

sequenceDiagram
    participant Kafka
    participant CouponService
    participant CouponDB
    participant ProcessedEventsDB

    Kafka->>CouponService: COUPON_RESTORE ์ด๋ฒคํŠธ ์ˆ˜์‹ 
    CouponService->>ProcessedEventsDB: ๋ฉฑ๋“ฑ์„ฑ ํ™•์ธ (event_id ์กฐํšŒ)
    alt ์ด๋ฏธ ์ฒ˜๋ฆฌ๋œ ์ด๋ฒคํŠธ
        CouponService-->>Kafka: ์Šคํ‚ต
    end
    CouponService->>CouponDB: ์ฟ ํฐ ์กฐํšŒ
    alt ๋งŒ๋ฃŒ๋œ ์ฟ ํฐ
        CouponService->>ProcessedEventsDB: event_id ์ €์žฅ
        Note over CouponService: ๋ณต๊ตฌ ๋ถˆ๊ฐ€ ๋กœ๊ทธ ๊ธฐ๋ก
    end
    CouponService->>CouponDB: status = UNUSED ๋ณต๊ตฌ
    CouponDB-->>CouponService: ์™„๋ฃŒ
    CouponService->>ProcessedEventsDB: event_id ์ €์žฅ

5. ๋งŒ๋ฃŒ ์Šค์ผ€์ค„๋Ÿฌ

sequenceDiagram
    participant Scheduler
    participant CouponDB

    Note over Scheduler: ๋งค์ผ ์ž์ • ์‹คํ–‰
    Scheduler->>CouponDB: UPDATE p_user_coupons SET status = EXPIRED
    Note over Scheduler,CouponDB: WHERE status = UNUSED AND expired_at < NOW()
    CouponDB-->>Scheduler: ์™„๋ฃŒ

6. Redis ๋ณต๊ตฌ ๋กœ์ง (์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ์‹œ)

sequenceDiagram
    participant CouponService
    participant CouponDB
    participant Redis

    Note over CouponService: @PostConstruct ์„œ๋ฒ„ ์‹œ์ž‘ ์‹œ
    CouponService->>CouponDB: ์ „์ฒด ์ฟ ํฐ ์กฐํšŒ
    loop ์ฟ ํฐ๋ณ„
        CouponService->>CouponDB: COUNT(p_user_coupons) WHERE coupon_id = ?
        CouponDB-->>CouponService: ๋ฐœ๊ธ‰๋œ ์ˆ˜ ๋ฐ˜ํ™˜
        CouponService->>Redis: SET coupon:stock:{couponId} (totalQuantity - issued)
    end
    Redis-->>CouponService: ์žฌ์„ธํŒ… ์™„๋ฃŒ