/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.data.repository

import com.runicgateway.app.core.result.ApiResult
import com.runicgateway.app.core.result.safeApiCall
import com.runicgateway.app.data.api.NotificationsApi
import com.runicgateway.app.data.api.dto.NotificationStreamsDto
import com.runicgateway.app.data.api.dto.NotificationSubscriptionsDto
import com.runicgateway.app.data.api.dto.PushDeviceDto
import com.runicgateway.app.data.api.dto.RegisterDeviceRequest
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Device registration + per-user stream subscriptions over the opt-in push surface
 * (PLAN.md §11, M7 Part 2). Every call returns a typed [ApiResult] so the screen
 * and the [com.runicgateway.app.core.push.PushManager] degrade gracefully — a `400`
 * (endpoint off the shard's allow-set) or a down backend never throws (§7).
 */
@Singleton
class NotificationsRepository @Inject constructor(
    private val api: NotificationsApi,
) {
    suspend fun registerDevice(endpoint: String, platform: String?): ApiResult<PushDeviceDto> =
        safeApiCall { api.registerDevice(RegisterDeviceRequest(endpoint = endpoint, platform = platform)) }

    suspend fun listDevices(): ApiResult<List<PushDeviceDto>> = safeApiCall { api.listDevices() }

    suspend fun deleteDevice(id: Long): ApiResult<Unit> = safeApiCall { api.deleteDevice(id) }

    suspend fun streams(): ApiResult<NotificationStreamsDto> = safeApiCall { api.streams() }

    suspend fun subscriptions(): ApiResult<NotificationSubscriptionsDto> =
        safeApiCall { api.subscriptions() }

    suspend fun setSubscriptions(streams: List<String>): ApiResult<NotificationSubscriptionsDto> =
        safeApiCall { api.putSubscriptions(NotificationSubscriptionsDto(streams)) }
}
