# skfh5970841/medimate-scheduler architecture

> Architecture diagram of the GitHub repository skfh5970841/medimate-scheduler, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/skfh5970841/medimate-scheduler
- Repository: https://github.com/skfh5970841/medimate-scheduler

## Overview

##### Project Type and Purpose
- This is a full-stack web application built with Next.js (React + Node) intended to run on Firebase Hosting/Functions (a “Firebase Studio” starter).
- Frontend: React pages and UI components (Tailwind CSS) under src/app and src/components.
- Backend: Next.js API routes (serverless functions) under src/app/api handling data, schedules, dispenser mappings, ESP32 LED control, and AI instance calls.
- External systems: OpenAI (via src/ai/ai-instance.ts) and ESP32 microcontroller devices (HTTP endpoints).

##### High-Level Architecture Components
a. Client (Browser)
b. Frontend (Next.js React App)
c. Serverless Backend (Next.js API Routes → deployed as Firebase Functions)
d. Data Store (JSON files in dev → Firestore/RTDB in prod)
e. AI Service (OpenAI API)
f. ESP32 Devices (HTTP-driven LED control)
g. Optional Authentication (Firebase Auth via .env keys)

##### Relationships and Data Flows
- The Browser renders React pages/components (src/app/layout.tsx → page.tsx → UI components).
- UI components invoke client-side hooks (src/hooks/use-toast.ts, use-mobile.tsx) or call Next.js API endpoints (/api/…) via fetch/Axios.
- API routes receive HTTP requests, perform business logic (src/lib/user-actions.ts, src/lib/ledState.ts, src/lib/utils.ts), read/write JSON or Firestore, call external services.
- For AI-driven features, API routes import and use src/ai/ai-instance.ts to forward prompts to OpenAI.
- For device control, API route “esp32/led/route.ts” issues HTTP commands to ESP32 over the local network or cloud endpoint.
- Scheduled tasks and supplement management APIs coordinate read/write operations on schedules.json and supplements.json (or their DB equivalents).

##### Architectural Patterns & Principles
- Separation of Concerns:
- UI layer (React + Tailwind)
- API layer (Next.js serverless functions)
- Data layer (dev JSON → production DB)
- Integration layer (AI, hardware)
- Component-Based Design: each UI widget is isolated under src/components/ui.
- Service Abstraction: business logic/services under src/services and src/lib.
- Progressive Enhancement: client and server share TypeScript types in src/types.ts.
- Scalability: easy to replace JSON files with Firestore collections and to scale serverless functions.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client (Browser)" 
        direction TB
        Browser["Web Browser"]:::frontend
    end

    %% Frontend Layer
    subgraph "Next.js Frontend (Firebase Hosting)" 
        direction TB
        PageLayout["Layout (src/app/layout.tsx)"]:::frontend
        PageMain["Main Page (src/app/page.tsx)"]:::frontend
        PageMapping["Mapping Page (src/app/mapping/page.tsx)"]:::frontend
        PageSuppAdmin["Supplements Admin Page (src/app/supplements-admin/page.tsx)"]:::frontend
        subgraph "UI Components (src/components/ui)" 
            direction TB
            UIlib["Generic UI Library"]:::frontend
        end
        LoginComp["Login Component"]:::frontend
        MappingModal["Mapping Modal"]:::frontend
        ScheduleModal["Schedule Modal"]:::frontend
        ScheduleWidget["Schedule Page Widget"]:::frontend
        Icons["Icons Collection"]:::frontend
        subgraph "Client-side Hooks"
            direction TB
            HookMobile["use-mobile.tsx"]:::frontend
            HookToast["use-toast.ts"]:::frontend
        end
    end

    %% Serverless Backend Layer
    subgraph "API / Firebase Functions" 
        direction TB
        API_DM["Dispenser Mapping API\n(/api/dispenser-mapping)"]:::api
        API_Sch["Schedules API\n(/api/schedules)"]:::api
        API_SchID["Schedule by ID API\n(/api/schedules/[id])"]:::api
        API_Sup["Supplements API\n(/api/supplements)"]:::api
        API_LED["ESP32 LED Control API\n(/api/esp32/led)"]:::api
        subgraph "Business Logic / Services"
            direction TB
            ServiceLED["LED State Utilities"]:::backend
            ServiceUA["User Actions"]:::backend
            ServiceUtils["Generic Utilities"]:::backend
            ServiceSup["Supplements Service"]:::backend
        end
        subgraph "AI Integration"
            direction TB
            AIInst["OpenAI Instance\n(src/ai/ai-instance.ts)"]:::external
            AIMock["Dev AI Mocks"]:::external
        end
    end

    %% Data Layer
    subgraph "Data Store"
        direction TB
        DB_Dev["JSON Data Files"]:::database
        DB_Prod["Firestore/Realtime DB"]:::database
    end

    %% External Systems
    subgraph "External Services"
        direction TB
        OpenAI["OpenAI API"]:::external
        ESP32["ESP32 Devices"]:::device
    end

    %% Dependencies
    Browser -->|"renders SSR/CSR"| PageLayout
    PageLayout --> PageMain
    PageLayout --> PageMapping
    PageLayout --> PageSuppAdmin
    PageMain -->|uses| UIlib
    PageMapping -->|uses| MappingModal
    PageMapping -->|uses| ScheduleWidget
    PageSuppAdmin -->|uses| ScheduleModal
    PageMain -->|calls| HookMobile
    PageMain -->|calls| HookToast

    PageMain -->|fetch /api/...| API_DM
    PageMain -->|fetch /api/...| API_Sch
    PageMain -->|fetch /api/...| API_Sup
    PageMain -->|fetch /api/...| API_LED

    API_DM -->|read/write| DB_Dev
    API_DM -->|read/write| DB_Prod
    API_Sch -->|read/write| DB_Dev
    API_Sch -->|read/write| DB_Prod
    API_SchID -->|read/write| DB_Dev
    API_SchID -->|read/write| DB_Prod
    API_Sup -->|read/write| DB_Dev
    API_Sup -->|read/write| DB_Prod

    API_LED -->|commands| ESP32

    API_DM -->|uses| ServiceUA
    API_Sch -->|uses| ServiceUA
    API_SchID -->|uses| ServiceUA
    API_Sup -->|uses| ServiceSup
    API_LED -->|uses| ServiceLED
    API_DM -->|utils| ServiceUtils
    API_Sch -->|utils| ServiceUtils
    API_SchID -->|utils| ServiceUtils
    API_Sup -->|utils| ServiceUtils

    API_Sup -->|calls| AIInst
    API_Sup -->|calls| AIMock
    AIInst -->|requests| OpenAI

    %% Scheduled Jobs
    API_Sch -->|-- scheduled -->| DB_Prod

    %% Click Events for component mapping
    click PageMain "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/page.tsx"
    click PageLayout "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/layout.tsx"
    click PageMapping "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/mapping/page.tsx"
    click PageSuppAdmin "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/supplements-admin/page.tsx"
    click UIlib "https://github.com/skfh5970841/medimate-scheduler/tree/master/src/components/ui/"
    click LoginComp "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/components/Login.tsx"
    click MappingModal "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/components/MappingModal.tsx"
    click ScheduleModal "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/components/ScheduleModal.tsx"
    click ScheduleWidget "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/components/SchedulePage.tsx"
    click Icons "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/components/icons.ts"
    click HookMobile "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/hooks/use-mobile.tsx"
    click HookToast "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/hooks/use-toast.ts"
    click API_DM "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/api/dispenser-mapping/route.ts"
    click API_Sch "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/api/schedules/route.ts"
    click API_SchID "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/api/schedules/[id]/route.ts"
    click API_Sup "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/api/supplements/route.ts"
    click API_LED "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/app/api/esp32/led/route.ts"
    click ServiceLED "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/lib/ledState.ts"
    click ServiceUA "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/lib/user-actions.ts"
    click ServiceUtils "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/lib/utils.ts"
    click ServiceSup "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/services/supplements.ts"
    click AIInst "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/ai/ai-instance.ts"
    click AIMock "https://github.com/skfh5970841/medimate-scheduler/blob/master/src/ai/dev.ts"
    click DB_Dev "https://github.com/skfh5970841/medimate-scheduler/tree/master/src/data/"
    click DB_Prod "https://github.com/skfh5970841/medimate-scheduler/tree/master/Firestore/Realtime Database"
    click OpenAI "https://github.com/skfh5970841/medimate-scheduler/tree/master/OpenAI API"
    click ESP32 "https://github.com/skfh5970841/medimate-scheduler/tree/master/ESP32 Devices"

    %% Styles
    classDef frontend fill:#a8f0c6,stroke:#000;
    classDef api fill:#c6daf5,stroke:#000;
    classDef backend fill:#c6e0f5,stroke:#000;
    classDef database fill:#f5c6a8,stroke:#000;
    classDef external fill:#d9c6f5,stroke:#000;
    classDef device fill:#e0e0e0,stroke:#000;
```
