# Entity Relationship Diagram

## Core Domain

```mermaid
erDiagram
    users ||--o{ tenants : owns
    users ||--o{ bookings : "books as customer"
    users ||--o{ reviews : writes
    users ||--o{ personal_access_tokens : has

    tenants ||--|{ venues : has
    tenants ||--|{ fields : has
    tenants ||--|{ bookings : receives
    tenants ||--|{ daily_reports : generates

    venues ||--|{ fields : contains

    fields ||--|{ bookings : "booked on"
    fields ||--|{ price_rules : has
    fields }o--o{ amenities : "field_amenities"

    bookings ||--|{ booking_items : contains
    bookings ||--o| payment_transactions : has
    bookings ||--o| reviews : has

    users {
        bigint id PK
        string name
        string email UK
        string phone UK
        enum role
        boolean is_active
    }

    tenants {
        bigint id PK
        bigint owner_id FK
        string business_name
        decimal latitude
        decimal longitude
        boolean is_active
        json settings
    }

    venues {
        bigint id PK
        bigint tenant_id FK
        string name
        json operating_hours
    }

    fields {
        bigint id PK
        bigint tenant_id FK
        bigint venue_id FK
        string name
        decimal price_per_hour
        json price_peak
        boolean is_active
    }

    bookings {
        bigint id PK
        string booking_code UK
        bigint field_id FK
        bigint tenant_id FK
        bigint customer_id FK
        date booking_date
        time start_time
        time end_time
        enum status
        decimal total_amount
    }

    payment_transactions {
        bigint id PK
        bigint booking_id FK
        string order_id UK
        string status
        decimal amount
    }

    reviews {
        bigint id PK
        bigint booking_id FK
        bigint field_id FK
        bigint customer_id FK
        tinyint rating
        text comment
    }
```

## Supporting Tables

```mermaid
erDiagram
    roles ||--|{ model_has_roles : assigns
    permissions ||--|{ role_has_permissions : grants
    roles ||--|{ role_has_permissions : has

    users ||--o{ app_notifications : receives
    users ||--o{ failed_jobs : "triggered by"

    price_rules {
        bigint id PK
        bigint field_id FK
        tinyint day_of_week
        time start_time
        time end_time
        decimal price_multiplier
    }

    daily_reports {
        bigint id PK
        bigint tenant_id FK
        date date
        int total_booking
        decimal revenue
        decimal occupancy_rate
    }

    amenities {
        bigint id PK
        string name
        string icon
    }
```

## Key Relationships

| From | To | Cardinality | Notes |
|------|----|-------------|-------|
| User | Tenant | 1:N | Owner owns tenants |
| Tenant | Venue | 1:N | Business locations |
| Venue | Field | 1:N | Courts/fields per venue |
| User | Booking | 1:N | Customer bookings |
| Field | Booking | 1:N | Slot reservations |
| Booking | PaymentTransaction | 1:1 | Midtrans payment |
| Booking | Review | 1:1 | Post-completion review |
| Field | PriceRule | 1:N | Dynamic pricing |
| Field | Amenity | N:M | Via `field_amenities` |

## Status Flows

### Booking Status
`pending` → `confirmed` → `paid` → `completed`
         ↘ `cancelled` / `no_show` / `expired`

### Payment Status
`pending` → `settlement` (paid) / `expire` / `cancel`
