erDiagram
  users ||--o{ workflow_requests : initiates
  users ||--o{ approval_levels : approves
  users ||--o{ participants : participates
  users ||--o{ work_notes : posts
  users ||--o{ documents : uploads
  users ||--o{ activities : performs
  users ||--o{ notifications : receives
  users ||--o{ user_sessions : has
  workflow_requests ||--|{ approval_levels : has
  workflow_requests ||--o{ participants : involves
  workflow_requests ||--o{ documents : contains
  workflow_requests ||--o{ work_notes : has
  workflow_requests ||--o{ activities : logs
  workflow_requests ||--o{ tat_tracking : monitors
  workflow_requests ||--o{ notifications : triggers
  workflow_requests ||--|| conclusion_remarks : concludes
  workflow_requests ||--|| dealer_claim_details : claim_details
  workflow_requests ||--|| dealer_proposal_details : proposal_details
  dealer_proposal_details ||--o{ dealer_proposal_cost_items : cost_items
  workflow_requests ||--|| dealer_completion_details : completion_details
  workflow_requests ||--|| internal_orders : internal_order
  workflow_requests ||--|| claim_budget_tracking : budget_tracking
  workflow_requests ||--|| claim_invoices : claim_invoice
  workflow_requests ||--|| claim_credit_notes : claim_credit_note
  work_notes ||--o{ work_note_attachments : has
  notifications ||--o{ email_logs : sends
  notifications ||--o{ sms_logs : sends
  workflow_requests ||--o{ report_cache : caches
  workflow_requests ||--o{ audit_logs : audits
  workflow_requests ||--o{ workflow_templates : templates
  users ||--o{ system_settings : updates

  users {
    uuid user_id PK
    varchar employee_id
    varchar okta_sub
    varchar email
    varchar first_name
    varchar last_name
    varchar display_name
    varchar department
    varchar designation
    varchar phone
    varchar manager
    varchar second_email
    text job_title
    varchar employee_number
    varchar postal_address
    varchar mobile_phone
    jsonb ad_groups
    jsonb location
    boolean is_active
    enum role
    timestamp last_login
    timestamp created_at
    timestamp updated_at
  }

  workflow_requests {
    uuid request_id PK
    varchar request_number
    uuid initiator_id FK
    varchar template_type
    varchar title
    text description
    enum priority
    enum status
    integer current_level
    integer total_levels
    decimal total_tat_hours
    timestamp submission_date
    timestamp closure_date
    text conclusion_remark
    text ai_generated_conclusion
    boolean is_draft
    boolean is_deleted
    timestamp created_at
    timestamp updated_at
  }

  approval_levels {
    uuid level_id PK
    uuid request_id FK
    integer level_number
    varchar level_name
    uuid approver_id FK
    varchar approver_email
    varchar approver_name
    decimal tat_hours
    integer tat_days
    enum status
    timestamp level_start_time
    timestamp level_end_time
    timestamp action_date
    text comments
    text rejection_reason
    boolean is_final_approver
    decimal elapsed_hours
    decimal remaining_hours
    decimal tat_percentage_used
    timestamp created_at
    timestamp updated_at
  }

  participants {
    uuid participant_id PK
    uuid request_id FK
    uuid user_id FK
    varchar user_email
    varchar user_name
    enum participant_type
    boolean can_comment
    boolean can_view_documents
    boolean can_download_documents
    boolean notification_enabled
    uuid added_by FK
    timestamp added_at
    boolean is_active
  }

  documents {
    uuid document_id PK
    uuid request_id FK
    uuid uploaded_by FK
    varchar file_name
    varchar original_file_name
    varchar file_type
    varchar file_extension
    bigint file_size
    varchar file_path
    varchar storage_url
    varchar mime_type
    varchar checksum
    boolean is_google_doc
    varchar google_doc_url
    enum category
    integer version
    uuid parent_document_id
    boolean is_deleted
    integer download_count
    timestamp uploaded_at
  }

  work_notes {
    uuid note_id PK
    uuid request_id FK
    uuid user_id FK
    varchar user_name
    varchar user_role
    text message
    varchar message_type
    boolean is_priority
    boolean has_attachment
    uuid parent_note_id
    uuid[] mentioned_users
    jsonb reactions
    boolean is_edited
    boolean is_deleted
    timestamp created_at
    timestamp updated_at
  }

  work_note_attachments {
    uuid attachment_id PK
    uuid note_id FK
    varchar file_name
    varchar file_type
    bigint file_size
    varchar file_path
    varchar storage_url
    boolean is_downloadable
    integer download_count
    timestamp uploaded_at
  }

  activities {
    uuid activity_id PK
    uuid request_id FK
    uuid user_id FK
    varchar user_name
    varchar activity_type
    text activity_description
    varchar activity_category
    varchar severity
    jsonb metadata
    boolean is_system_event
    varchar ip_address
    text user_agent
    timestamp created_at
  }

  notifications {
    uuid notification_id PK
    uuid user_id FK
    uuid request_id FK
    varchar notification_type
    varchar title
    text message
    boolean is_read
    enum priority
    varchar action_url
    boolean action_required
    jsonb metadata
    varchar[] sent_via
    boolean email_sent
    boolean sms_sent
    boolean push_sent
    timestamp read_at
    timestamp expires_at
    timestamp created_at
  }

  tat_tracking {
    uuid tracking_id PK
    uuid request_id FK
    uuid level_id FK
    varchar tracking_type
    enum tat_status
    decimal total_tat_hours
    decimal elapsed_hours
    decimal remaining_hours
    decimal percentage_used
    boolean threshold_50_breached
    timestamp threshold_50_alerted_at
    boolean threshold_80_breached
    timestamp threshold_80_alerted_at
    boolean threshold_100_breached
    timestamp threshold_100_alerted_at
    integer alert_count
    timestamp last_calculated_at
  }

  conclusion_remarks {
    uuid conclusion_id PK
    uuid request_id FK
    text ai_generated_remark
    varchar ai_model_used
    decimal ai_confidence_score
    text final_remark
    uuid edited_by FK
    boolean is_edited
    integer edit_count
    jsonb approval_summary
    jsonb document_summary
    text[] key_discussion_points
    timestamp generated_at
    timestamp finalized_at
  }

  audit_logs {
    uuid audit_id PK
    uuid user_id FK
    varchar entity_type
    uuid entity_id
    varchar action
    varchar action_category
    jsonb old_values
    jsonb new_values
    text changes_summary
    varchar ip_address
    text user_agent
    varchar session_id
    varchar request_method
    varchar request_url
    integer response_status
    integer execution_time_ms
    timestamp created_at
  }

  user_sessions {
    uuid session_id PK
    uuid user_id FK
    varchar session_token
    varchar refresh_token
    varchar ip_address
    text user_agent
    varchar device_type
    varchar browser
    varchar os
    timestamp login_at
    timestamp last_activity_at
    timestamp logout_at
    timestamp expires_at
    boolean is_active
    varchar logout_reason
  }

  email_logs {
    uuid email_log_id PK
    uuid request_id FK
    uuid notification_id FK
    varchar recipient_email
    uuid recipient_user_id FK
    text[] cc_emails
    text[] bcc_emails
    varchar subject
    text body
    varchar email_type
    varchar status
    integer send_attempts
    timestamp sent_at
    timestamp failed_at
    text failure_reason
    timestamp opened_at
    timestamp clicked_at
    timestamp created_at
  }

  sms_logs {
    uuid sms_log_id PK
    uuid request_id FK
    uuid notification_id FK
    varchar recipient_phone
    uuid recipient_user_id FK
    text message
    varchar sms_type
    varchar status
    integer send_attempts
    timestamp sent_at
    timestamp delivered_at
    timestamp failed_at
    text failure_reason
    varchar sms_provider
    varchar sms_provider_message_id
    decimal cost
    timestamp created_at
  }

  system_settings {
    uuid setting_id PK
    varchar setting_key
    text setting_value
    varchar setting_type
    varchar setting_category
    text description
    boolean is_editable
    boolean is_sensitive
    jsonb validation_rules
    text default_value
    uuid updated_by FK
    timestamp created_at
    timestamp updated_at
  }

  workflow_templates {
    uuid template_id PK
    varchar template_name
    text template_description
    varchar template_category
    jsonb approval_levels_config
    decimal default_tat_hours
    boolean is_active
    integer usage_count
    uuid created_by FK
    timestamp created_at
    timestamp updated_at
  }

  report_cache {
    uuid cache_id PK
    varchar report_type
    jsonb report_params
    jsonb report_data
    uuid generated_by FK
    timestamp generated_at
    timestamp expires_at
    integer access_count
    timestamp last_accessed_at
  }

  dealer_claim_details {
    uuid claim_id PK
    uuid request_id
    varchar activity_name
    varchar activity_type
    varchar dealer_code
    varchar dealer_name
    varchar dealer_email
    varchar dealer_phone
    text dealer_address
    date activity_date
    varchar location
    date period_start_date
    date period_end_date
    timestamp created_at
    timestamp updated_at
  }

  dealer_proposal_details {
    uuid proposal_id PK
    uuid request_id
    string proposal_document_path
    string proposal_document_url
    decimal total_estimated_budget
    string timeline_mode
    date expected_completion_date
    int expected_completion_days
    text dealer_comments
    date submitted_at
    timestamp created_at
    timestamp updated_at
  }

  dealer_proposal_cost_items {
    uuid cost_item_id PK
    uuid proposal_id FK
    uuid request_id FK
    string item_description
    decimal amount
    int item_order
    timestamp created_at
    timestamp updated_at
  }

  dealer_completion_details {
    uuid completion_id PK
    uuid request_id
    date activity_completion_date
    int number_of_participants
    decimal total_closed_expenses
    date submitted_at
    timestamp created_at
    timestamp updated_at
  }

  dealer_completion_expenses {
    uuid expense_id PK
    uuid request_id
    uuid completion_id
    string description
    decimal amount
    timestamp created_at
    timestamp updated_at
  }

  internal_orders {
    uuid io_id PK
    uuid request_id
    string io_number
    text io_remark
    decimal io_available_balance
    decimal io_blocked_amount
    decimal io_remaining_balance
    uuid organized_by FK
    date organized_at
    string sap_document_number
    enum status
    timestamp created_at
    timestamp updated_at
  }

  claim_budget_tracking {
    uuid budget_id PK
    uuid request_id
    decimal initial_estimated_budget
    decimal proposal_estimated_budget
    date proposal_submitted_at
    decimal approved_budget
    date approved_at
    uuid approved_by FK
    decimal io_blocked_amount
    date io_blocked_at
    decimal closed_expenses
    date closed_expenses_submitted_at
    decimal final_claim_amount
    date final_claim_amount_approved_at
    uuid final_claim_amount_approved_by FK
    decimal credit_note_amount
    date credit_note_issued_at
    enum budget_status
    string currency
    decimal variance_amount
    decimal variance_percentage
    uuid last_modified_by FK
    date last_modified_at
    text modification_reason
    timestamp created_at
    timestamp updated_at
  }

  claim_invoices {
    uuid invoice_id PK
    uuid request_id
    string invoice_number
    date invoice_date
    string dms_number
    decimal amount
    string status
    text description
    timestamp created_at
    timestamp updated_at
  }

  claim_credit_notes {
    uuid credit_note_id PK
    uuid request_id
    string credit_note_number
    date credit_note_date
    decimal credit_note_amount
    string status
    text reason
    text description
    timestamp created_at
    timestamp updated_at
  }

