# Database Entity Relationship Diagram (ERD)

This document visualizes the database structure and relationships based on the current Prisma schema.

## Overview
The database is designed for a multi-tenant SaaS architecture where `business_profiles` is the central entity.
- **Users**: Can own businesses or be team members.
- **Business Profile**: Contains all business-specific data (Consultations, Automations, Contacts).
- **Automation**: Rules and logs for chat automation.
- **Communication**: Conversations, Messages, and Campaigns.

## ERD (Mermaid)

```mermaid
erDiagram
    USERS ||--o{ BUSINESS_PROFILES : owns
    USERS ||--o{ TEAM_MEMBERS : "is member of"
    USERS ||--o{ AUTH_TOKENS : "has"
    USERS ||--o{ NOTIFICATIONS : "receives"
    
    BUSINESS_PROFILES ||--o{ AI_AGENTS : has
    BUSINESS_PROFILES ||--o{ AUTOMATION_RULES : defines
    BUSINESS_PROFILES ||--o{ CONTACTS : manages
    BUSINESS_PROFILES ||--o{ CONVERSATION_ANALYTICS : tracks
    BUSINESS_PROFILES ||--o{ CAMPAIGNS : runs
    BUSINESS_PROFILES ||--o{ BUSINESS_DASHBOARD_CONFIG : configures
    
    CONTACTS ||--o{ CONVERSATIONS : participates
    CONTACTS ||--o{ CONTACT_TAGS : "tagged with"
    
    CONVERSATIONS ||--o{ MESSAGES : contains
    CONVERSATIONS ||--o{ AUTOMATION_LOGS : triggers
    
    AUTOMATION_RULES ||--o{ AUTOMATION_LOGS : produces
    
    AI_AGENTS ||--o{ AI_CONVERSATIONS_CONTEXT : maintains
    AI_AGENTS ||--o{ AI_KNOWLEDGE_BASE : uses

    DASHBOARD_WIDGETS ||--o{ BUSINESS_DASHBOARD_CONFIG : "enabled in"

    %% Detailed Entities
    USERS {
        uuid id PK
        string email
        string password_hash
        string name
        string role
    }

    BUSINESS_PROFILES {
        uuid id PK
        uuid owner_id FK
        string name
        string business_type
        string subscription_plan
    }

    CONTACTS {
        uuid id PK
        uuid business_id FK
        string phone
        string email
        string name
        string lead_status
    }

    CONVERSATIONS {
        uuid id PK
        uuid contact_id FK
        uuid assigned_to FK
        string status
        string channel
    }

    MESSAGES {
        uuid id PK
        uuid conversation_id FK
        string content
        string sender_type
        timestamp created_at
    }

    AUTOMATION_RULES {
        uuid id PK
        uuid business_id FK
        string trigger_type
        string action_config
    }

    DASHBOARD_WIDGETS {
        uuid id PK
        string widget_key UK
        string default_config
    }

    BUSINESS_DASHBOARD_CONFIG {
        uuid id PK
        uuid business_id FK
        string widget_key FK
        int position
        json custom_config
    }
```

## Analysis of Relationships

1.  **Multi-Tenancy**: The `business_id` column presence in almost all operational tables (`contacts`, `conversations`, `automation_rules`) correctly enforces data isolation between tenants.
2.  **User-Business**: The distinction between `users` (auth identity) and `business_profiles` (tenant entity) allows for future scalability (e.g., one user managing multiple businesses).
3.  **Dashboard Configuration**: The Many-to-Many relationship between `business_profiles` and `dashboard_widgets` (mediated by `business_dashboard_config`) is effectively implemented, allowing per-business customization of the dashboard.
