/*
 * 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.PublicApi
import com.runicgateway.app.data.api.dto.PageDto
import com.runicgateway.app.data.api.dto.PostDto
import javax.inject.Inject
import javax.inject.Singleton

/**
 * News posts and CMS pages (PLAN.md §6.1). URL post categories map 1:1 to the
 * backend's route segments (`news | five-on-friday | newsletter | screenshots`).
 */
@Singleton
class ContentRepository @Inject constructor(
    private val api: PublicApi,
) {
    /** Known URL categories, in display order. */
    enum class PostCategory(val urlSlug: String) {
        NEWS("news"),
        FIVE_ON_FRIDAY("five-on-friday"),
        NEWSLETTER("newsletter"),
        SCREENSHOTS("screenshots"),
        ;

        companion object {
            fun fromUrlSlug(slug: String?): PostCategory? = entries.firstOrNull { it.urlSlug == slug }
        }
    }

    suspend fun getPosts(category: PostCategory): ApiResult<List<PostDto>> =
        safeApiCall { api.getPosts(category.urlSlug) }

    suspend fun getPost(category: PostCategory, idOrSlug: String): ApiResult<PostDto> =
        safeApiCall { api.getPost(category.urlSlug, idOrSlug) }

    suspend fun getPage(slug: String): ApiResult<PageDto> =
        safeApiCall { api.getPage(slug) }
}
