import XCTest
@testable import MealMood

@MainActor
final class OnboardingViewModelTests: XCTestCase {

    func testAddDishAllowsNameOnlyAndUsesDefaultTag() {
        let vm = OnboardingViewModel()
        vm.newDishName = "Test dish"
        let fallbackId = UUID()

        vm.addDish(defaultTagId: fallbackId)

        XCTAssertEqual(vm.addedDishes.count, 1)
        XCTAssertEqual(vm.addedDishes.first?.name, "Test dish")
        XCTAssertEqual(vm.addedDishes.first?.tagIds, [fallbackId])
    }

    func testAddDishPreventsDuplicateNames() {
        let vm = OnboardingViewModel()
        vm.newDishName = "Lentils"
        vm.addDish(defaultTagId: UUID())

        vm.newDishName = "  lentils  "
        vm.addDish(defaultTagId: UUID())

        XCTAssertEqual(vm.addedDishes.count, 1)
    }

    func testSuggestedDishFallsBackToFirstTagWhenNoNameMatch() {
        let vm = OnboardingViewModel()
        let fallback = Tag(name: "Carne", nameEN: "Meat", color: "#E74C3C", sortOrder: 0)
        let other = Tag(name: "Pescado", nameEN: "Fish", color: "#3498DB", sortOrder: 1)

        vm.addSuggestedDish(
            name: "Roast chicken",
            preferredTagNames: ["Unknown tag"],
            availableTags: [other, fallback]
        )

        XCTAssertEqual(vm.addedDishes.count, 1)
        XCTAssertEqual(vm.addedDishes.first?.tagIds, [fallback.id])
    }
}
