@startuml "System Komponenten"

scale 0.7
skinparam dpi 600
skinparam backgroundColor #FFAB40

skinparam componentStyle uml2
skinparam packageStyle rectangle
skinparam defaultFontSize 10

title Monorepo - System Komponenten

' ===== ROW 1: FRONTEND =====
package "apps/client (Expo React Native)" as ClientPkg #87CEEB {
    package "Screens (app/)" {
        [Login/Register] as AuthScreens
        [Calendar View] as CalendarScreen
        [Chat View] as ChatScreen
        [Settings] as SettingsScreen
        [Edit Event] as EditEventScreen
        [Event Detail] as EventDetail
        [Note Editor] as NoteScreen
    }

    package "Services" {
        [API Client] as ApiClient
        [Auth Service] as ClientAuth
        [Event Service] as ClientEvent
        [Chat Service] as ClientChat
        [Caldav Config Service] as ClientCaldav
    }

    package "Components" {
        [UI Components] as UIComponents
        [Event Cards] as EventCards
        [Auth Guard] as AuthGuard
    }

    package "Stores" {
        [Auth Store] as AuthStore
        [Events Store] as EventsStore
        [Chat Store] as ChatStore
        [Theme Store] as ThemeStore
    }
}

' ===== ROW 2: SHARED (centered) =====
package "packages/shared" as SharedPkg #DDA0DD {
    [Models] as SharedModels
    [DTOs] as SharedDTOs
    [Utils] as SharedUtils
}

' ===== ROW 3: BACKEND =====
package "apps/server (Express.js)" as ServerPkg #98FB98 {
    package "Controller Layer" {
        [Routes] as Routes
        [Controllers] as Controllers
        [Middleware] as Middleware
    }

    package "Service Layer" {
        [Interfaces] as Interfaces
        [AuthService] as AuthSvc
        [ChatService] as ChatSvc
        [EventService] as EventSvc
        [CaldavService] as CaldavSvc
    }

    package "AI Implementations" {
        [GPTAdapter] as GPT
    }

    package "Data Access Implementations" {
        [Repositories] as Repos
        [Mongoose Schemas] as Schemas
    }

    package "Utils" {
        [Password] as PwdUtil
        [RecurrenceExpander] as RecExpander
        [EventFormatters] as EvtFormatters
    }
}

' ===== ROW 4: EXTERNAL =====
database "MongoDB" as MongoDB
cloud "OpenAI API" as OpenAIAPI
cloud "CalDAV Server" as CaldavServer

' ===== CONNECTIONS =====

' Frontend: Screens -> Services
AuthScreens --> ClientAuth
CalendarScreen --> ClientEvent
CalendarScreen --> ClientCaldav
ChatScreen --> ClientChat
SettingsScreen --> ClientCaldav
EditEventScreen --> ClientEvent
EventDetail --> ClientEvent
NoteScreen --> ClientEvent

ClientAuth --> ApiClient
ClientEvent --> ApiClient
ClientChat --> ApiClient
ClientCaldav --> ApiClient

ApiClient --> AuthStore
ClientEvent --> EventsStore
ClientChat --> ChatStore

' Frontend: Auth
AuthGuard --> AuthStore
AuthGuard --> ClientCaldav
AuthScreens --> ClientCaldav

' Frontend: Screens -> Components
CalendarScreen --> EventCards
ChatScreen --> EventCards
ChatScreen --> UIComponents

' Frontend -> Shared -> Backend (HTTP flow)
ApiClient --> SharedDTOs : sends
SharedDTOs --> Routes : HTTP/REST

' Shared types used by both sides
SharedModels ..> ApiClient : used by
SharedDTOs ..> Controllers : used by
SharedModels ..> Repos : used by

' Backend: Controller Layer
Routes --> Middleware
Routes --> Controllers

' Backend: Controller -> Service
Controllers --> AuthSvc
Controllers --> ChatSvc
Controllers --> EventSvc
Controllers --> CaldavSvc

' Backend: Service -> Interfaces
AuthSvc --> Interfaces
ChatSvc --> Interfaces
EventSvc --> Interfaces
CaldavSvc --> Interfaces

' Backend: Service dependencies
ChatSvc --> EventSvc
CaldavSvc --> EventSvc

' Backend: AI & Data Access implement Interfaces
GPT ..|> Interfaces
Repos ..|> Interfaces

' Backend: Service -> Utils
AuthSvc --> PwdUtil
EventSvc --> RecExpander
ChatSvc --> EvtFormatters

' Backend: Data Access
Repos --> Schemas

' Backend -> External
Schemas --> MongoDB
GPT --> OpenAIAPI
CaldavSvc --> CaldavServer

@enduml
