package de.tsschulz.timeclock.data.settings

import de.tsschulz.timeclock.data.api.InvitationDto
import de.tsschulz.timeclock.data.api.InviteRequest
import de.tsschulz.timeclock.data.api.PasswordChangeRequest
import de.tsschulz.timeclock.data.api.ProfileDto
import de.tsschulz.timeclock.data.api.ProfileUpdateRequest
import de.tsschulz.timeclock.data.api.StateDto
import de.tsschulz.timeclock.data.api.TimeClockApiClient
import de.tsschulz.timeclock.data.api.TimewishCreateRequest
import de.tsschulz.timeclock.data.api.TimewishDto
import de.tsschulz.timeclock.data.api.WatcherDto
import de.tsschulz.timeclock.data.api.WatcherRequest

class SettingsRepository(
    private val api: TimeClockApiClient,
) {
    suspend fun getProfile(): ProfileDto = api.getProfile()
    suspend fun getStates(): List<StateDto> = api.getStates()
    suspend fun updateProfile(fullName: String, stateId: String?, weekWorkdays: Int, dailyHours: Double, preferredTitleType: Int) =
        api.updateProfile(ProfileUpdateRequest(fullName, stateId, weekWorkdays, dailyHours, preferredTitleType))

    suspend fun createGoogleLinkUrl(): String {
        val response = api.createGoogleLinkUrl()
        if (!response.success || response.url.isNullOrBlank()) {
            throw IllegalStateException(response.error ?: "Google-Verknüpfung konnte nicht gestartet werden")
        }
        return response.url
    }

    suspend fun changePassword(oldPassword: String, newPassword: String, confirmPassword: String) =
        api.changePassword(PasswordChangeRequest(oldPassword, newPassword, confirmPassword))

    suspend fun getTimewishes(): List<TimewishDto> = api.getTimewishes()
    suspend fun createTimewish(day: Int, wishtype: Int, hours: Double?, startDate: String, endDate: String?) =
        api.createTimewish(TimewishCreateRequest(day, wishtype, hours, startDate, endDate))
    suspend fun deleteTimewish(id: String) = api.deleteTimewish(id)

    suspend fun getInvites(): List<InvitationDto> = api.getInvites()
    suspend fun sendInvite(email: String) = api.sendInvite(InviteRequest(email))

    suspend fun getWatchers(): List<WatcherDto> = api.getWatchers()
    suspend fun addWatcher(email: String) = api.addWatcher(WatcherRequest(email))
    suspend fun deleteWatcher(id: String) = api.deleteWatcher(id)
}
