# WhatsApp SaaS - Project Overview

## 1. Introduction
This project is a **Monolithic** WhatsApp Automation & Marketing SaaS platform designed to serve various business types (Agencies, Shops, Real Estate, Influencers, etc.). It enables businesses to automate customer interactions, manage leads, and broadcast messages via WhatsApp, powered by AI and a dynamic dashboard system.

> **Important:** While some reference documents might mention microservices from earlier iterations, this project strictly follows a **Monolithic Architecture** using a Unified Server (Node.js/Express) and a single PostgreSQL database (managed via Prisma).

## 2. Core Architecture

### Backend (`backend/unified_server`)
-   **Runtime:** Node.js
-   **Framework:** Express.js
-   **ORM:** Prisma
-   **Database:** PostgreSQL (with `pgcrypto` for UUIDs)
-   **Queue:** RabbitMQ (for message processing and broadcasting, if scale requires)
-   **Cache:** Redis (recommended for session management and rate limiting)

### Frontend (`frontend`)
-   **Framework:** React
-   **Styling:** Tailwind CSS
-   **State Management:** Context API / Zustand
-   **Dashboard:** Dynamic widget-based system

## 3. Key Modules

### A. Authentication & User Management
-   **Dual Registration:** Supports Email/Password (Web) and Phone Number (WhatsApp OTP).
-   **Multi-Tenancy:** Data isolation per business profile.
-   **Teams:** Role-based access (Owner, Admin, Manager, Agent).

### B. Dynamic Dashboard
-   **Concept:** The dashboard adapts based on the `business_type` (e.g., Shop vs. Real Estate) and `primary_use_case` (e.g., Sales vs. Support).
-   **Widgets:** KPI Cards (Messages Today), Charts (Lead Funnel), Lists (Recent Chats).
-   **Configuration:** Stored in database (`business_dashboard_config`).

### C. Unified Messaging (CRM)
-   **Channels:** Primarily WhatsApp (Meta Cloud API), designed to be extensible (Web Chat, SMS).
-   **Capabilities:**
    -   Unified Inbox.
    -   Rich Media Support (Images, Audio, Docs).
    -   AI vs. Human Handoff.

### D. AI & Automation
-   **Real-time AI:** Context-aware replies using LLMs (OpenAI/Anthropic).
-   **Rules Engine:**
    -   **Keyword:** "Price" -> Sends pricing info.
    -   **Intent:** "Buying" -> Triggers sales flow.
-   **Knowledge Base:** AI RAG (Retrieval-Augmented Generation) from business data.

### E. Business Entities
-   **Generic Table:** `business_entities` stores diverse items like **Products** (Shops) or **Properties** (Real Estate).
-   **Flexible:** Uses JSONB for custom fields specific to the entity type.

## 4. Database Schema Highlights
-   **`business_profiles`**: Central table for tenant configuration.
-   **`integrations`**: Manages connections (WhatsApp, etc.).
-   **`conversations` & `messages`**: Channel-agnostic messaging tables.
-   **`automation_rules`**: Logic for automated responses.

## 5. Deployment
-   **Infrastructure:** Hostinger VPS (Ubuntu).
-   **Process Management:** PM2 for Node.js.
-   **Reverse Proxy:** Nginx.
