package ru.myitschool.work.data.repo

import ru.myitschool.work.data.dto.BookRequestDto
import ru.myitschool.work.data.dto.RoomBookingRequestDto
import ru.myitschool.work.data.source.NetworkDataSource
import ru.myitschool.work.domain.room.entities.RoomEntity

class RoomBookingRepository(
    private val authRepository: AuthRepository
) {
    suspend fun getRoomBookings(roomId: Int): Result<RoomEntity> {
        val token = authRepository.getToken() ?: return getNoAuthResult()
        return NetworkDataSource.getRoomBookings(token, roomId = roomId).mapCatching { dto ->
            RoomEntity(name=dto.name, data=dto.data)
        }
    }

    suspend fun deleteRoomBooking(roomId: Int, date: String): Result<Boolean> {
        val token = authRepository.getToken() ?: return getNoAuthResult()
        val data = RoomBookingRequestDto(roomId = roomId, date = date)
        return NetworkDataSource.deleteRoomBooking(token, data)
    }

    suspend fun sendRoomBooking(roomId: Int, date: String): Result<Boolean> {
        val token = authRepository.getToken() ?: return getNoAuthResult()
        val dto = BookRequestDto(date, roomId)
        return NetworkDataSource.addBook(token, dto)
    }
    private fun <T> getNoAuthResult() = Result.failure<T>(
        IllegalStateException("No auth")
    )
}