/*
 * Copyright 2025 - 2026 PICO. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pico.spatialui.sample.viewmodel

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class SearchViewModelTest {

    @Test
    fun recommendedKeys_areDistinctAndRemovable() {
        val vm = SearchViewModel()
        val initial = vm.searchKeyRecommendedList.value
        assertEquals(6, initial.size)
        assertEquals(initial.size, initial.toSet().size)

        vm.removeKeyFromRecommendedList(initial[0])
        val updated = vm.searchKeyRecommendedList.value
        assertEquals(5, updated.size)
        assertFalse(updated.contains(initial[0]))
    }

    @Test
    fun toggleCategorySelection_flipsOnlyTarget() {
        val vm = SearchViewModel()
        val before = vm.categoryList.value.associate { it.nameRes to it.isSelected }
        val targetId = before.keys.first()
        vm.toggleCategorySelection(targetId)
        val after = vm.categoryList.value.associate { it.nameRes to it.isSelected }

        // Target flipped
        assertEquals(!(before[targetId] ?: false), after[targetId])
        // Others unchanged
        for ((name, selected) in before) {
            if (name != targetId) {
                assertEquals(selected, after[name])
            }
        }
    }

    @Test
    fun searchResults_haveSixItemsWithDistinctIds() {
        val vm = SearchViewModel()
        val results = vm.searchResultList.value
        assertEquals(6, results.size)
        assertEquals(results.size, results.map { it.id }.toSet().size)
        assertTrue(results.all { it.id.startsWith("id_") })
    }
}
