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

import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * Decode tests for the wiki DTOs (`/public/wiki*`). Summary rows omit the body;
 * the detail page carries tags, backlinks, and unresolved ("red") link targets.
 */
class WikiDtoTest {

    private val json = Json {
        ignoreUnknownKeys = true
        explicitNulls = false
        coerceInputValues = true
    }

    @Test fun summaryRowDecodesWithCategory() {
        val dto = json.decodeFromString<WikiSummaryDto>(
            """{"id":5,"slug":"pvp","title":"PvP","excerpt":"combat",
                "category_slug":"systems","category_title":"Systems","updated_at":"2026-07-20T00:00:00Z"}""",
        )
        assertEquals(5L, dto.id)
        assertEquals("systems", dto.categorySlug)
        assertEquals("Systems", dto.categoryTitle)
        assertEquals("combat", dto.excerpt)
    }

    @Test fun pageDecodesTagsBacklinksAndMissingLinks() {
        val dto = json.decodeFromString<WikiPageDto>(
            """{
                "id":9,"slug":"housing","title":"Housing","body":"<p>text</p>",
                "category_slug":"systems","category_title":"Systems",
                "tags":[{"slug":"idoc","label":"IDOC"}],
                "backlinks":[{"slug":"pvp","title":"PvP"}],
                "missing_links":["nonexistent-page"]
            }""",
        )
        assertEquals("<p>text</p>", dto.body)
        assertEquals(1, dto.tags.size)
        assertEquals("IDOC", dto.tags[0].label)
        assertEquals("pvp", dto.backlinks[0].slug)
        assertEquals(listOf("nonexistent-page"), dto.missingLinks)
    }

    @Test fun pageDefaultsCollectionsWhenAbsent() {
        val dto = json.decodeFromString<WikiPageDto>("""{"id":1,"slug":"x","title":"X"}""")
        assertTrue(dto.tags.isEmpty())
        assertTrue(dto.backlinks.isEmpty())
        assertTrue(dto.missingLinks.isEmpty())
    }

    @Test fun categoryAndTagDecodePublishedCounts() {
        val cat = json.decodeFromString<WikiCategoryDto>(
            """{"id":2,"slug":"systems","title":"Systems","description":"d","published_count":12}""",
        )
        assertEquals(12L, cat.publishedCount)
        val tag = json.decodeFromString<WikiTagDto>("""{"id":4,"slug":"idoc","label":"IDOC","published_count":3}""")
        assertEquals(3L, tag.publishedCount)
    }
}
