# muneebtech07/mental-health-support-chatbot architecture

> Architecture diagram of the GitHub repository muneebtech07/mental-health-support-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/muneebtech07/mental-health-support-chatbot
- Repository: https://github.com/muneebtech07/mental-health-support-chatbot

## Overview

##### Project Type and Purpose
- This is a full-stack application designed to provide mental health support via a chatbot. The primary purpose is to offer users guided conversations and resources (through the Gemini API) while tracking various mental health-related metrics (mood, exercises, goals, etc.).
- Key points from the README include the integration with the Gemini API for natural language understanding, separate frontend and backend processes, and a range of UI modules for tasks like mood tracking and exercise details.

##### Analyzing the File Structure and Architectural Clues
- The file tree shows two main parts:
a. Backend: The “server” folder (server/index.js) which is implemented in Node.js (as indicated by the README using Express.js). This will handle communication with both the frontend and external services (i.e., Gemini API).
b. Frontend: The “src” directory contains a React-based application (using Vite), with several components focused on various mental health functions (Chat, MoodTracker, GoalTracker, etc.). The presence of files like App.tsx, index.css, and main.tsx suggest a modern React/Vite setup.
- Additional configuration files (like .env, tailwind.config.js, postcss.config.js) show that styling (TailwindCSS) and environment management are also considered.
- The “supabase” folder with migration scripts hints at database integration (likely for user data or chat history) even though it’s not deeply detailed in the README.
- There is also an “api.ts” and “supabase.ts” service file under src/services, indicating the separation of API communication logic and database (Supabase) interactions.

##### Key Components
a. Frontend
- Components (e.g., Chat, ChatHistory, CrisisSupport, MeditationPlayer, and others).
- The main React application entry point (App.tsx and main.tsx) that brings together these UI elements.
- Integration with external APIs via services (e.g., API, Supabase).
- UI modules for various personalized features like mood tracking, goal tracking, etc.
b. Backend
- The Express.js server (server/index.js) responsible for handling API requests, serving resources, and interfacing with the Gemini API.
- Endpoints for chat functionality and mental health resources (e.g., the “/resources” endpoint as mentioned in the README).
c. External Systems and Services
- Gemini API for natural language processing and chat responses.
- Database integration using Supabase (with migration scripts under the “supabase” folder) for user data persistence and resource tracking.
d. Configuration and Deployment
- Environmental configuration via .env, ensuring sensitive API keys are managed securely.
- Deployment details such as frontend hosted on services like Vercel/Netlify and backend on Heroku, AWS, or other Node.js-compatible platforms.

##### Relationships and Data Flow
- Frontend (React/Vite app) initiating API calls to the backend (Express.js server) on port 3000.
- The backend making calls to the Gemini API to generate chat responses.
- Frontend components communicating with backend endpoints to fetch resources, update mood histories, log user interactions, etc.
- The backend interacting with the database (Supabase) for data persistence.
- If applicable, show how the application handles CORS to facilitate communication between the two distinct ports (5176 for frontend and 3000 for backend).

##### Architectural Patterns and Design Principles
- Separation of concerns by clearly demarcating UI components in the frontend from business logic in the backend.
- Use of service layers (as seen in src/services) that abstract API calls and database interactions.
- Modular design principles used (multiple reusable React components for UI, distinct API modules on the backend).

## Diagram (Mermaid)

```mermaid
flowchart TD 
    %% Frontend Subgraph
    subgraph "Frontend (React/Vite Application) [Port 5176]"
        FMain["Main (React/Vite)"]:::frontend
        FApp["App (React/Vite)"]:::frontend
        subgraph "UI Components"
            Chat["Chat Interface"]:::frontend
            ChatHistory["Chat History"]:::frontend
            Crisis["Crisis Support"]:::frontend
            ExerciseDetail["Exercise Detail"]:::frontend
            Exercises["Exercises"]:::frontend
            GoalTracker["Goal Tracker"]:::frontend
            MoodTracker["Mood Tracker"]:::frontend
            Meditation["Meditation Player"]:::frontend
            Login["Login"]:::frontend
            Progress["Progress"]:::frontend
            Sidebar["Sidebar"]:::frontend
            ThoughtRecord["Thought Record"]:::frontend
            UserHeader["User Header"]:::frontend
            WelcomeModal["Welcome Modal"]:::frontend
        end
        subgraph "Frontend Services"
            APIService["API Service"]:::frontend
            SupabaseService["Supabase Service"]:::frontend
        end
    end

    %% Backend Subgraph
    subgraph "Backend (Express.js Server) [Port 3000]"
        Backend["Express.js Server"]:::backend
    end

    %% External Systems Subgraph
    subgraph "External Systems"
        Gemini["Gemini API"]:::external
        Database["Database (Supabase)"]:::external
        Migration["Migration (peaceful_butterfly.sql)"]:::external
    end

    %% Configuration & Deployment Subgraph
    subgraph "Configuration & Deployment"
        Env["Environment (.env)"]:::config
        Tailwind["Tailwind Config"]:::config
        Postcss["PostCSS Config"]:::config
        Vite["Vite Config"]:::config
        Tsconfig["TS Config Files"]:::config
    end

    %% Connections between Frontend components
    FMain -->|"bootstraps"| FApp
    FApp -->|"initiates REST API calls"| Backend
    APIService -->|"forwards requests"| Backend
    SupabaseService -->|"queries/writes data"| Database

    %% UI Components interactions with Services
    Chat -->|"sends chat"| APIService
    ChatHistory -->|"fetches history"| APIService
    Crisis -->|"requests crisis info"| APIService
    ExerciseDetail -->|"fetches details"| APIService
    Exercises -->|"retrieves exercises"| APIService
    GoalTracker -->|"tracks goals"| APIService
    MoodTracker -->|"logs mood"| APIService
    Meditation -->|"plays media"| APIService
    Login -->|"handles auth"| APIService
    Progress -->|"updates progress"| APIService
    Sidebar -->|"navigates"| APIService
    ThoughtRecord -->|"records thoughts"| APIService
    UserHeader -->|"displays user info"| APIService
    WelcomeModal -->|"welcomes user"| APIService

    %% Backend interactions with External Systems
    Backend -->|"calls NLP"| Gemini
    Backend -->|"persists data"| Database

    %% Styling Definitions
    classDef frontend fill:#add8e6,stroke:#000,stroke-width:1px;
    classDef backend fill:#90ee90,stroke:#000,stroke-width:1px;
    classDef external fill:#ffcc99,stroke:#000,stroke-width:1px;
    classDef config fill:#ffff99,stroke:#000,stroke-width:1px;

    %% Click Events with Component Mapping
    click FMain "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/main.tsx"
    click FApp "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/App.tsx"
    click Chat "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/Chat.tsx"
    click ChatHistory "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/ChatHistory.tsx"
    click Crisis "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/CrisisSupport.tsx"
    click ExerciseDetail "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/ExerciseDetail.tsx"
    click Exercises "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/Exercises.tsx"
    click GoalTracker "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/GoalTracker.tsx"
    click MoodTracker "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/MoodTracker.tsx"
    click Meditation "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/MeditationPlayer.tsx"
    click Login "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/Login.tsx"
    click Progress "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/Progress.tsx"
    click Sidebar "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/Sidebar.tsx"
    click ThoughtRecord "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/ThoughtRecord.tsx"
    click UserHeader "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/UserHeader.tsx"
    click WelcomeModal "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/components/WelcomeModal.tsx"
    click APIService "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/services/api.ts"
    click SupabaseService "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/src/services/supabase.ts"
    click Backend "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/server/index.js"
    click Migration "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/supabase/migrations/20250113122509_peaceful_butterfly.sql"
    click Env "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/.env"
    click Tailwind "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/tailwind.config.js"
    click Postcss "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/postcss.config.js"
    click Vite "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/vite.config.ts"
    click Tsconfig "https://github.com/muneebtech07/mental-health-support-chatbot/blob/main/tsconfig.json"
```
