# Order Service Architecture

> Architecture documentation for Order Service - the orchestration brain.

## Architecture Overview

```mermaid
graph TB
    subgraph "API Layer"
        C[Controllers]
        CMD[Commands]
        Q[Queries]
    end
    
    subgraph "Application Layer"
        SF[Strategy Factory]
        RS[RetailStrategy]
        SS[ServiceStrategy]
        FS[FnbStrategy]
    end
    
    subgraph "Domain Layer"
        O[Order Aggregate]
        OI[OrderItem]
        OS[OrderStatus]
    end
    
    subgraph "External Services"
        INV[Inventory Service]
        BOOK[Booking Service]
        FNB[F&B Engine]
    end
    
    C --> CMD
    CMD --> SF
    SF --> RS --> INV
    SF --> SS --> BOOK
    SF --> FS --> FNB
    CMD --> O
    
    style O fill:#50c878,stroke:#2d8659,color:#fff
    style SF fill:#9b59b6,stroke:#7d3c98,color:#fff
```

## Strategy Pattern Implementation

```mermaid
classDiagram
    class ILineItemStrategy {
        <<interface>>
        +SupportedType ProductType
        +ValidateAsync(OrderItem, Guid) Task
        +ExecuteAsync(OrderItem, Guid) Task
    }
    
    class RetailStrategy {
        +SupportedType = Physical
        +ValidateAsync() CheckStock
        +ExecuteAsync() DeductStock
    }
    
    class ServiceStrategy {
        +SupportedType = Service
        +ValidateAsync() CheckSlot
        +ExecuteAsync() CreateAppointment
    }
    
    class FnbStrategy {
        +SupportedType = PreparedFood
        +ValidateAsync() Always OK
        +ExecuteAsync() CreateKitchenTicket
    }
    
    ILineItemStrategy <|.. RetailStrategy
    ILineItemStrategy <|.. ServiceStrategy
    ILineItemStrategy <|.. FnbStrategy
```

## Order State Machine

```mermaid
stateDiagram-v2
    [*] --> Draft: Create Order
    Draft --> Validated: All Items Valid
    Validated --> Paid: Payment Success
    Paid --> Processing: Execute Items
    Processing --> Completed: All Items Done
    
    Draft --> Cancelled: Cancel
    Validated --> Cancelled: Cancel
    Paid --> Cancelled: Refund
```

## Database Schema

```mermaid
erDiagram
    orders {
        uuid id PK
        uuid shop_id FK
        uuid customer_id FK
        varchar status
        decimal total_amount
        timestamp created_at
    }
    
    order_items {
        uuid id PK
        uuid order_id FK
        uuid product_id FK
        varchar product_type
        int quantity
        decimal unit_price
        varchar status
        jsonb metadata
    }
    
    orders ||--o{ order_items : contains
```

## Order Processing Workflow

```mermaid
sequenceDiagram
    participant Handler as CreateOrderHandler
    participant Factory as StrategyFactory
    participant Strategies as ILineItemStrategy[]
    participant Payment as WalletService
    
    Handler->>Factory: GetStrategies()
    
    rect rgb(200, 230, 200)
        Note over Handler,Strategies: Phase 1: Validation
        loop Each Item
            Handler->>Factory: GetStrategy(item.Type)
            Factory-->>Handler: ILineItemStrategy
            Handler->>Strategies: ValidateAsync(item)
        end
    end
    
    Handler->>Payment: ProcessPayment(order)
    Payment-->>Handler: Success
    
    rect rgb(200, 200, 230)
        Note over Handler,Strategies: Phase 2: Execution
        loop Each Item
            Handler->>Strategies: ExecuteAsync(item)
        end
    end
```

## Integration Points

| Service | Protocol | Purpose |
|---------|----------|---------|
| Catalog Service | HTTP | Get product details |
| Merchant Service | HTTP | Get shop features |
| Wallet Service | HTTP | Process payment |
| Inventory Service | HTTP | Stock operations |
| Booking Service | HTTP | Appointments |
| F&B Engine | Event Bus | Kitchen tickets |

## References

- [Multi-vertical Architecture](../../../../docs/en/architecture/multi-vertical-architecture.md)
- [Strategy Pattern](https://refactoring.guru/design-patterns/strategy)
