/*
 * 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.WikiCategoryDto
import com.runicgateway.app.data.api.dto.WikiPageDto
import com.runicgateway.app.data.api.dto.WikiSummaryDto
import com.runicgateway.app.data.api.dto.WikiTagDto
import javax.inject.Inject
import javax.inject.Singleton

/** Wiki listing/filtering, categories, tags, and detail (PLAN.md §6.1). */
@Singleton
class WikiRepository @Inject constructor(
    private val api: PublicApi,
) {
    /** Full-text [query] takes precedence over [category]/[tag] on the backend. */
    suspend fun getPages(
        query: String? = null,
        category: String? = null,
        tag: String? = null,
    ): ApiResult<List<WikiSummaryDto>> =
        safeApiCall { api.getWikiPages(query?.takeIf { it.isNotBlank() }, category, tag) }

    suspend fun getCategories(): ApiResult<List<WikiCategoryDto>> =
        safeApiCall { api.getWikiCategories() }

    suspend fun getTags(): ApiResult<List<WikiTagDto>> =
        safeApiCall { api.getWikiTags() }

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