erDiagram
    BUSINESS_EVENT_LOG {
        int id PK
        datetime timestamp
        string event_type
        int tenant_id
        string trace_id
        string span_id
        string span_name
        string parent_span_id
        int document_version_id
        float document_version_file_size
        int specialist_id
        string specialist_type
        string specialist_type_version
        string chat_session_id
        int interaction_id
        string environment
        int llm_metrics_total_tokens
        int llm_metrics_prompt_tokens
        int llm_metrics_completion_tokens
        float llm_metrics_total_time
        int llm_metrics_nr_of_pages
        int llm_metrics_call_count
        string llm_interaction_type
        text message
        int license_usage_id FK
    }

    LICENSE {
        int id PK
        int tenant_id FK
        int tier_id FK
        date start_date
        date end_date
        int nr_of_periods
        string currency
        boolean yearly_payment
        float basic_fee
        int max_storage_mb
        float additional_storage_price
        int additional_storage_bucket
        int included_embedding_mb
        decimal additional_embedding_price
        int additional_embedding_bucket
        int included_interaction_tokens
        decimal additional_interaction_token_price
        int additional_interaction_bucket
        float overage_embedding
        float overage_interaction
        boolean additional_storage_allowed
        boolean additional_embedding_allowed
        boolean additional_interaction_allowed
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    LICENSE_TIER {
        int id PK
        string name
        string version
        date start_date
        date end_date
        float basic_fee_d
        float basic_fee_e
        int max_storage_mb
        decimal additional_storage_price_d
        decimal additional_storage_price_e
        int additional_storage_bucket
        int included_embedding_mb
        decimal additional_embedding_price_d
        decimal additional_embedding_price_e
        int additional_embedding_bucket
        int included_interaction_tokens
        decimal additional_interaction_token_price_d
        decimal additional_interaction_token_price_e
        int additional_interaction_bucket
        float standard_overage_embedding
        float standard_overage_interaction
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    PARTNER_SERVICE_LICENSE_TIER {
        int partner_service_id PK,FK
        int license_tier_id PK,FK
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    LICENSE_PERIOD {
        int id PK
        int license_id FK
        int tenant_id FK
        int period_number
        date period_start
        date period_end
        string currency
        float basic_fee
        int max_storage_mb
        float additional_storage_price
        int additional_storage_bucket
        int included_embedding_mb
        decimal additional_embedding_price
        int additional_embedding_bucket
        int included_interaction_tokens
        decimal additional_interaction_token_price
        int additional_interaction_bucket
        boolean additional_storage_allowed
        boolean additional_embedding_allowed
        boolean additional_interaction_allowed
        enum status
        datetime upcoming_at
        datetime pending_at
        datetime active_at
        datetime completed_at
        datetime invoiced_at
        datetime closed_at
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    LICENSE_USAGE {
        int id PK
        int tenant_id FK
        float storage_mb_used
        float embedding_mb_used
        int embedding_prompt_tokens_used
        int embedding_completion_tokens_used
        int embedding_total_tokens_used
        int interaction_prompt_tokens_used
        int interaction_completion_tokens_used
        int interaction_total_tokens_used
        int license_period_id FK
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    PAYMENT {
        int id PK
        int license_period_id FK
        int tenant_id FK
        enum payment_type
        decimal amount
        string currency
        text description
        enum status
        string external_payment_id
        string payment_method
        jsonb provider_data
        datetime paid_at
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    INVOICE {
        int id PK
        int license_period_id FK
        int payment_id FK
        int tenant_id FK
        enum invoice_type
        string invoice_number
        date invoice_date
        date due_date
        decimal amount
        string currency
        decimal tax_amount
        text description
        enum status
        datetime sent_at
        datetime paid_at
        datetime created_at
        int created_by FK
        datetime updated_at
        int updated_by FK
    }

    LICENSE_CHANGE_LOG {
        int id PK
        int license_id FK
        datetime changed_at
        string field_name
        string old_value
        string new_value
        text reason
        int created_by FK
    }

    TENANT {
        int id PK
        string name
        string currency
    }

    USER {
        int id PK
        string user_name
        string email
    }

    PARTNER_SERVICE {
        int id PK
        string name
        string type
    }

    %% Main business relationships
    TENANT ||--o{ LICENSE : "has many"
    LICENSE_TIER ||--o{ LICENSE : "has many"
    LICENSE ||--o{ LICENSE_PERIOD : "has many"
    LICENSE_PERIOD ||--|| LICENSE_USAGE : "has one"
    LICENSE_PERIOD ||--o{ PAYMENT : "has many"
    LICENSE_PERIOD ||--o{ INVOICE : "has many"
    
    %% License management
    LICENSE ||--o{ LICENSE_CHANGE_LOG : "has many"
    
    %% Payment-Invoice relationship
    PAYMENT ||--o| INVOICE : "can have"
    
    %% Partner service licensing
    PARTNER_SERVICE ||--o{ PARTNER_SERVICE_LICENSE_TIER : "has many"
    LICENSE_TIER ||--o{ PARTNER_SERVICE_LICENSE_TIER : "has many"
    
    %% Event logging
    LICENSE_USAGE ||--o{ BUSINESS_EVENT_LOG : "has many"
    
    %% Tenant relationships
    TENANT ||--o{ LICENSE_PERIOD : "has many"
    TENANT ||--o{ LICENSE_USAGE : "has many"
    TENANT ||--o{ PAYMENT : "has many"
    TENANT ||--o{ INVOICE : "has many"