package de.trainingstagebuch.app.services

import de.trainingstagebuch.app.repository.DiaryActionResult
import de.trainingstagebuch.app.repository.DiaryAccidentsResult
import de.trainingstagebuch.app.repository.DiaryActivitiesResult
import de.trainingstagebuch.app.repository.DiaryAddActivityResult
import de.trainingstagebuch.app.repository.DiaryActivityParticipantsResult
import de.trainingstagebuch.app.repository.DomainSocketEvent
import de.trainingstagebuch.app.repository.DiaryCreateTagResult
import de.trainingstagebuch.app.repository.DiaryParticipantActionResult
import de.trainingstagebuch.app.repository.DiaryParticipantsResult
import de.trainingstagebuch.app.repository.MemberActivityStatsResult
import de.trainingstagebuch.app.repository.MemberNotesResult
import de.trainingstagebuch.app.repository.DiaryCreateDateResult
import de.trainingstagebuch.app.repository.DiaryDatesResult
import de.trainingstagebuch.app.repository.DiaryGroupsResult
import de.trainingstagebuch.app.repository.DiaryPlanResult
import de.trainingstagebuch.app.repository.DiaryTagsResult
import de.trainingstagebuch.app.repository.MembersResult
import de.trainingstagebuch.app.repository.PredefinedActivitiesResult
import de.trainingstagebuch.app.repository.TrainingstagebuchRepository
import kotlinx.coroutines.flow.SharedFlow

class DiaryService(
    private val repository: TrainingstagebuchRepository
) {
    val socketEvents: SharedFlow<DomainSocketEvent> = repository.socketDomainEvents

    suspend fun loadDiaryDates(clubId: String): DiaryDatesResult = repository.loadDiaryDates(clubId)

    suspend fun createDiaryDate(
        clubId: String,
        date: String,
        trainingStart: String?,
        trainingEnd: String?
    ): DiaryCreateDateResult = repository.createDiaryDate(clubId, date, trainingStart, trainingEnd)

    suspend fun updateDiaryTrainingTime(
        clubId: String,
        dateId: String,
        trainingStart: String?,
        trainingEnd: String?
    ): DiaryActionResult = repository.updateDiaryTrainingTime(clubId, dateId, trainingStart, trainingEnd)

    suspend fun deleteDiaryDate(clubId: String, dateId: String): DiaryActionResult =
        repository.deleteDiaryDate(clubId, dateId)

    suspend fun loadMembers(clubId: String): MembersResult = repository.loadMembers(clubId, showAll = false)

    suspend fun loadParticipants(dateId: String): DiaryParticipantsResult = repository.loadParticipants(dateId)

    suspend fun addParticipant(dateId: String, memberId: String): DiaryParticipantActionResult =
        repository.addParticipant(dateId, memberId)

    suspend fun removeParticipant(dateId: String, memberId: String): DiaryActionResult =
        repository.removeParticipant(dateId, memberId)

    suspend fun loadActivities(dateId: String): DiaryActivitiesResult = repository.loadActivities(dateId)

    suspend fun addActivity(dateId: String, description: String): DiaryAddActivityResult =
        repository.addActivity(dateId, description)

    suspend fun loadDiaryMemberNotes(clubId: String, diaryDateId: String, memberId: String): MemberNotesResult =
        repository.loadDiaryMemberNotes(clubId, diaryDateId, memberId)

    suspend fun addDiaryMemberNote(
        clubId: String,
        diaryDateId: String,
        memberId: String,
        content: String
    ): DiaryActionResult = repository.addDiaryMemberNote(clubId, diaryDateId, memberId, content)

    suspend fun deleteDiaryMemberNote(clubId: String, noteId: String): DiaryActionResult =
        repository.deleteDiaryMemberNote(clubId, noteId)

    suspend fun loadTags(): DiaryTagsResult = repository.loadTags()

    suspend fun createTag(name: String): DiaryCreateTagResult = repository.createTag(name)

    suspend fun addTagToDiaryDate(clubId: String, diaryDateId: String, tagId: String): DiaryActionResult =
        repository.addTagToDiaryDate(clubId, diaryDateId, tagId)

    suspend fun removeTagFromDiaryDate(clubId: String, diaryDateId: String, tagId: String): DiaryActionResult =
        repository.removeTagFromDiaryDate(clubId, diaryDateId, tagId)

    suspend fun loadDiaryPlan(clubId: String, dateId: String): DiaryPlanResult =
        repository.loadDiaryPlan(clubId, dateId)

    suspend fun addDiaryPlanItem(clubId: String, dateId: String, activity: String, orderId: Int): DiaryActionResult =
        repository.addDiaryPlanItem(clubId, dateId, activity, orderId)

    suspend fun updateDiaryPlanItem(clubId: String, planItemId: String, activity: String): DiaryActionResult =
        repository.updateDiaryPlanItem(clubId, planItemId, activity)

    suspend fun deleteDiaryPlanItem(clubId: String, planItemId: String): DiaryActionResult =
        repository.deleteDiaryPlanItem(clubId, planItemId)

    suspend fun updateDiaryPlanItemOrder(clubId: String, planItemId: String, order: Int): DiaryActionResult =
        repository.updateDiaryPlanItemOrder(clubId, planItemId, order)

    suspend fun loadDiaryGroups(clubId: String, dateId: String): DiaryGroupsResult =
        repository.loadDiaryGroups(clubId, dateId)

    suspend fun updateParticipantGroup(dateId: String, memberId: String, groupId: String?): DiaryActionResult =
        repository.updateParticipantGroup(dateId, memberId, groupId)

    suspend fun createDiaryGroup(clubId: String, dateId: String, name: String, lead: String): DiaryActionResult =
        repository.createDiaryGroup(clubId, dateId, name, lead)

    suspend fun updateDiaryGroup(groupId: String, clubId: String, dateId: String, name: String, lead: String): DiaryActionResult =
        repository.updateDiaryGroup(groupId, clubId, dateId, name, lead)

    suspend fun deleteDiaryGroup(groupId: String, clubId: String, dateId: String): DiaryActionResult =
        repository.deleteDiaryGroup(groupId, clubId, dateId)

    suspend fun createDiaryGroupActivity(
        clubId: String,
        dateId: String,
        groupId: String,
        activity: String,
        predefinedActivityId: String?
    ): DiaryActionResult = repository.createDiaryGroupActivity(clubId, dateId, groupId, activity, predefinedActivityId)

    suspend fun loadActivityParticipantIds(clubId: String, activityId: String): DiaryActivityParticipantsResult =
        repository.loadActivityParticipantIds(clubId, activityId)

    suspend fun assignActivityParticipants(clubId: String, activityId: String, participantIds: List<String>): DiaryActionResult =
        repository.assignActivityParticipants(clubId, activityId, participantIds)

    suspend fun removeActivityParticipant(clubId: String, activityId: String, participantId: String): DiaryActionResult =
        repository.removeActivityParticipant(clubId, activityId, participantId)

    suspend fun createAccident(clubId: String, dateId: String, accident: String): DiaryActionResult =
        repository.createAccident(clubId, dateId, accident)

    suspend fun loadAccidents(clubId: String, dateId: String): DiaryAccidentsResult =
        repository.loadAccidents(clubId, dateId)

    suspend fun loadMemberActivityStats(clubId: String, memberId: String): MemberActivityStatsResult =
        repository.loadMemberActivityStats(clubId, memberId)

    suspend fun searchPredefinedActivities(term: String, limit: Int = 10): PredefinedActivitiesResult =
        repository.searchPredefinedActivities(term, limit)
}
