# F&B Engine Architecture

> Architecture documentation for F&B Engine.

## Architecture Overview

```mermaid
graph TB
    subgraph "API Layer"
        TC[TableController]
        SC[SessionController]
        KC[KitchenController]
    end
    
    subgraph "Domain Layer"
        T[Table]
        S[Session]
        KT[KitchenTicket]
    end
    
    subgraph "Infrastructure"
        DB[(PostgreSQL)]
        EB[Event Bus]
    end
    
    TC --> T
    SC --> S
    KC --> KT
    S --> T
    KT --> S
    T --> DB
    KT --> EB
    
    style T fill:#50c878,stroke:#2d8659,color:#fff
    style S fill:#3498db,stroke:#2980b9,color:#fff
```

## Database Schema

```mermaid
erDiagram
    tables {
        uuid id PK
        uuid shop_id FK
        varchar table_number
        int capacity
        varchar zone
        varchar status
    }
    
    sessions {
        uuid id PK
        uuid table_id FK
        timestamp started_at
        timestamp closed_at
        int guest_count
        varchar status
    }
    
    kitchen_tickets {
        uuid id PK
        uuid session_id FK
        uuid order_item_id FK
        varchar station
        int priority
        varchar status
        timestamp created_at
    }
    
    tables ||--o{ sessions : has
    sessions ||--o{ kitchen_tickets : contains
```

## Kitchen Display Flow

```mermaid
sequenceDiagram
    participant Order as Order Service
    participant FnB as F&B Engine
    participant KDS as Kitchen Display
    
    Order->>FnB: KitchenTicketCreated Event
    FnB->>FnB: Save Ticket
    KDS->>FnB: Poll Pending Tickets
    FnB-->>KDS: Ticket List
    
    Note over KDS: Chef prepares order
    
    KDS->>FnB: Update Status (Done)
    FnB->>Order: TicketCompleted Event
```

## Table State Machine

```mermaid
stateDiagram-v2
    [*] --> Available
    Available --> Occupied: Open Session
    Occupied --> Available: Close Session
    Available --> Reserved: Reserve
    Reserved --> Occupied: Guest Arrived
    Reserved --> Available: Cancel
```

## References

- [Multi-vertical Architecture](../../../../docs/en/architecture/multi-vertical-architecture.md)
