@startuml "Backend Klassendiagramm"

scale 0.9
skinparam dpi 600
skinparam backgroundColor #FFAB40

skinparam packageStyle rectangle
skinparam classAttributeIconSize 0
skinparam classFontSize 11
skinparam defaultFontSize 10

top to bottom direction

title Backend Architektur - Klassendiagramm

package "Controller Layer" #ADD8E6 {
    class AuthController {
        ' +login()
        ' +register()
        ' +refresh()
        ' +logout()
    }

    class ChatController {
        ' +sendMessage()
        ' +confirmEvent()
        ' +rejectEvent()
        ' +getConversations()
        ' +getConversation()
    }

    class EventController {
        ' -pushToCaldav()
        ' -deleteFromCaldav()
        ' +create()
        ' +getById()
        ' +getAll()
        ' +getByDateRange()
        ' +update()
        ' +delete()
    }

    class CaldavController {
        ' +saveConfig()
        ' +loadConfig()
        ' +deleteConfig()
        ' +pullEvents()
        ' +pushEvents()
        ' +pushEvent()
    }

    class AuthMiddleware {
        ' +authenticate()
    }
}

package "Service Layer" #90EE90 {
    package "Interfaces" {
        interface AIProvider {
            ' +processMessage()
        }

        interface UserRepository {
            ' +findById()
            ' +findByEmail()
            ' +create()
        }

        interface EventRepository {
            ' +findById()
            ' +findByUserId()
            ' +findByDateRange()
            ' +findByCaldavUUID()
            ' +searchByTitle()
            ' +create()
            ' +update()
            ' +delete()
            ' +addExceptionDate()
        }

        interface ChatRepository {
            ' +getConversationsByUser()
            ' +createConversation()
            ' +getMessages()
            ' +createMessage()
            ' +updateProposalResponse()
            ' +updateProposalEvent()
        }

        interface CaldavRepository {
            ' +findByUserId()
            ' +createOrUpdate()
            ' +deleteByUserId()
        }
    }

    class AuthService {
        ' -userRepo: UserRepository
        ' +login()
        ' +register()
    }

    class ChatService {
        ' -chatRepo: ChatRepository
        ' -eventService: EventService
        ' -aiProvider: AIProvider
        ' +processMessage()
        ' +confirmEvent()
        ' +rejectEvent()
        ' +getConversations()
        ' +getConversation()
    }

    class EventService {
        ' -eventRepo: EventRepository
        ' +create()
        ' +getById()
        ' +getAll()
        ' +getByDateRange()
        ' +searchByTitle()
        ' +findByCaldavUUID()
        ' +update()
        ' +delete()
        ' +deleteRecurring()
    }

    class CaldavService {
        ' -caldavRepo: CaldavRepository
        ' -eventService: EventService
        ' +connect()
        ' +pullEvents()
        ' +pushEvent()
        ' +pushAll()
        ' +deleteEvent()
        ' +getConfig()
        ' +saveConfig()
        ' +deleteConfig()
    }
}

package "AI Implementations" #FFA07A {
    class GPTAdapter implements AIProvider {
        ' -apiKey: string
        ' +processMessage()
    }
}

package "Data Access Implementations" #FFD700 {
    class MongoUserRepository implements UserRepository {
        ' -model: UserModel
    }

    class MongoEventRepository implements EventRepository {
        ' -model: EventModel
    }

    class MongoChatRepository implements ChatRepository {
        ' -model: ChatModel
    }

    class MongoCaldavRepository implements CaldavRepository {
        ' -model: CaldavConfigModel
    }
}

package "Models" #D3D3D3 {
    class User <<Entity>> {
        ' +id: string
        ' +email: string
        ' +displayName: string
    }

    class CalendarEvent <<Entity>> {
        ' +id: string
        ' +userId: string
        ' +title: string
        ' +startTime: Date
        ' +endTime: Date
        ' +note?: string
    }

    class ChatMessage <<Entity>> {
        ' +id: string
        ' +sender: string
        ' +content: string
        ' +proposedEvent?: ProposedEvent
    }
}

package "Utils" #DDA0DD {
    class Password {
        ' +hash()
        ' +compare()
    }

    class RecurrenceExpander {
        ' +expandRecurringEvents()
    }

    class EventFormatters {
        ' +getWeeksOverview()
        ' +getMonthOverview()
    }
}

' Controller -> Service
AuthController --> AuthService
ChatController --> ChatService
ChatController --> CaldavService
EventController --> EventService
EventController --> CaldavService
CaldavController --> CaldavService
' Service -> Interfaces (intern)
AuthService --> UserRepository
ChatService --> ChatRepository
ChatService --> EventService
ChatService --> AIProvider
EventService --> EventRepository
CaldavService --> CaldavRepository
CaldavService --> EventService

' Auth uses Utils
AuthService --> Password

' Event/Chat uses Utils
EventService --> RecurrenceExpander
ChatService --> EventFormatters

@enduml
