# Booking Service Architecture

> Architecture documentation for Booking Service.

## Architecture Overview

```mermaid
graph TB
    subgraph "API Layer"
        SC[StaffController]
        RC[ResourceController]
        AC[AppointmentController]
    end
    
    subgraph "Domain Layer"
        SS[StaffSchedule]
        R[Resource]
        A[Appointment]
    end
    
    subgraph "Infrastructure"
        DB[(PostgreSQL)]
        REPO[Repositories]
    end
    
    SC --> SS
    RC --> R
    AC --> A
    A --> SS
    A --> R
    SS --> DB
    R --> DB
    A --> DB
    
    style A fill:#50c878,stroke:#2d8659,color:#fff
    style R fill:#3498db,stroke:#2980b9,color:#fff
```

## Database Schema

```mermaid
erDiagram
    staff_schedules {
        uuid id PK
        uuid staff_id FK
        uuid shop_id FK
        int day_of_week
        time start_time
        time end_time
    }
    
    resources {
        uuid id PK
        uuid shop_id FK
        varchar name
        varchar resource_type
        int capacity
    }
    
    appointments {
        uuid id PK
        uuid shop_id FK
        uuid customer_id FK
        uuid staff_id FK
        uuid resource_id FK
        uuid service_id FK
        timestamp start_time
        timestamp end_time
        varchar status
    }
    
    staff_schedules ||--o{ appointments : assigned
    resources ||--o{ appointments : booked
```

## Appointment State Machine

```mermaid
stateDiagram-v2
    [*] --> Scheduled: Book
    Scheduled --> Confirmed: Confirm
    Scheduled --> Cancelled: Cancel
    Confirmed --> InProgress: Check-in
    InProgress --> Completed: Complete
    Confirmed --> NoShow: No Show
    Confirmed --> Cancelled: Cancel
```

## Slot Finding Algorithm

```mermaid
flowchart TD
    START[Find Slots Request] --> GET_STAFF[Get Staff Schedules]
    GET_STAFF --> GET_RESOURCE[Get Resource Availability]
    GET_RESOURCE --> GET_APPTS[Get Existing Appointments]
    GET_APPTS --> INTERSECT[Calculate Intersection]
    INTERSECT --> REMOVE[Remove Booked Slots]
    REMOVE --> SLOTS[Return Available Slots]
```

## Integration with Order Service

```mermaid
sequenceDiagram
    participant Order as Order Service
    participant Strategy as ServiceStrategy
    participant Booking as Booking Service
    
    Order->>Strategy: ValidateAsync(item)
    Strategy->>Booking: Check Slot Available
    Booking-->>Strategy: Available
    
    Order->>Strategy: ExecuteAsync(item)
    Strategy->>Booking: Create Appointment
    Booking-->>Strategy: Appointment Created
```

## References

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