erDiagram
    TENANT {
        int id PK
        string code
        string name
        string website
        string timezone
        string type
        string currency
        boolean storage_dirty
        int default_tenant_make_id FK
        datetime created_at
        datetime updated_at
    }

    USER {
        int id PK
        int tenant_id FK
        string user_name
        string email
        string password
        string first_name
        string last_name
        boolean active
        string fs_uniquifier
        datetime confirmed_at
        date valid_to
        boolean is_primary_contact
        boolean is_financial_contact
        datetime last_login_at
        datetime current_login_at
        string last_login_ip
        string current_login_ip
        int login_count
        datetime created_at
        datetime updated_at
    }

    ROLE {
        int id PK
        string name
        string description
    }

    ROLES_USERS {
        int user_id PK,FK
        int role_id PK,FK
    }

    TENANT_DOMAIN {
        int id PK
        int tenant_id FK
        string domain
        date valid_to
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    TENANT_PROJECT {
        int id PK
        int tenant_id FK
        string name
        text description
        array services
        string encrypted_api_key
        string visual_api_key
        boolean active
        string responsible_email
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    TENANT_MAKE {
        int id PK
        int tenant_id FK
        string name
        text description
        boolean active
        string website
        string logo_url
        string default_language
        array allowed_languages
        jsonb chat_customisation_options
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    PARTNER {
        int id PK
        int tenant_id FK
        string code
        string logo_url
        boolean active
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    PARTNER_SERVICE {
        int id PK
        int partner_id FK
        string name
        text description
        string type
        string type_version
        boolean active
        json configuration
        json permissions
        json system_metadata
        json user_metadata
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    PARTNER_TENANT {
        int partner_service_id PK,FK
        int tenant_id PK,FK
        json configuration
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    PARTNER_SERVICE_LICENSE_TIER {
        int id PK
        int partner_service_id FK
    }

    SPECIALIST_MAGIC_LINK_TENANT {
        string magic_link_code PK
        int tenant_id FK
    }

    TRANSLATION_CACHE {
        string cache_key PK
        text source_text
        text translated_text
        string source_language
        string target_language
        text context
        int prompt_tokens
        int completion_tokens
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
        datetime last_used_at
    }

    PARTNER_RAG_RETRIEVER {
        int partner_id FK
        int tenant_id PK,FK
        int retriever_id PK
    }

    LICENSE {
        int id PK
        int tenant_id FK
        date start_date
        date end_date
    }

    LICENSE_USAGE {
        int id PK
        int tenant_id FK
    }

    %% Relationships
    TENANT ||--o{ USER : "has many"
    TENANT ||--o{ TENANT_DOMAIN : "has many"
    TENANT ||--o{ TENANT_PROJECT : "has many"
    TENANT ||--o{ TENANT_MAKE : "has many"
    TENANT ||--o| TENANT_MAKE : "default_tenant_make"
    TENANT ||--o| PARTNER : "has one"
    TENANT ||--o{ LICENSE : "has many"
    TENANT ||--o{ LICENSE_USAGE : "has many"
    TENANT ||--o{ SPECIALIST_MAGIC_LINK_TENANT : "has many"
    TENANT ||--o{ PARTNER_TENANT : "has many"
    TENANT ||--o{ PARTNER_RAG_RETRIEVER : "has many"

    USER ||--o{ ROLES_USERS : "has many"

    ROLE ||--o{ ROLES_USERS : "has many"

    PARTNER ||--o{ PARTNER_SERVICE : "has many"
    PARTNER ||--o{ PARTNER_RAG_RETRIEVER : "has many"

    PARTNER_SERVICE ||--o{ PARTNER_TENANT : "has many"
    PARTNER_SERVICE ||--o{ PARTNER_SERVICE_LICENSE_TIER : "has many"