import XCTest
@testable import MealMood

final class AutocompleteEngineTests: XCTestCase {

    func testValidateDropRejectsConsecutiveTag() {
        let tag = Tag(
            name: "Carne",
            nameEN: "Meat",
            color: "#E74C3C",
            maxPerWeek: nil,
            noConsecutive: true,
            noDuplicateInDay: false
        )

        let dishA = Dish(name: "Pollo", tagIds: [tag.id])
        let dishB = Dish(name: "Ternera", tagIds: [tag.id])

        let plan = WeekPlan(weekStartDate: Date().startOfWeek())
        let mondayDinner = MealSlot(dayOfWeek: 0, mealType: MealType.dinner.rawValue, dishId: dishA.id)
        let tuesdayDinner = MealSlot(dayOfWeek: 1, mealType: MealType.dinner.rawValue, dishId: nil)
        plan.slots = [mondayDinner, tuesdayDinner]

        let isValid = AutocompleteEngine.validateDrop(
            dish: dishB,
            slot: tuesdayDinner,
            plan: plan,
            allTags: [tag],
            allDishes: [dishA, dishB]
        )

        XCTAssertFalse(isValid)
    }

    func testAutocompleteLeavesSlotEmptyWhenMaxPerWeekReached() {
        let tag = Tag(
            name: "Pescado",
            nameEN: "Fish",
            color: "#3498DB",
            maxPerWeek: 1,
            noConsecutive: false,
            noDuplicateInDay: false
        )

        let fishDish = Dish(name: "Salmon", tagIds: [tag.id])
        let plan = WeekPlan(weekStartDate: Date().startOfWeek())

        let mondayDinner = MealSlot(dayOfWeek: 0, mealType: MealType.dinner.rawValue, dishId: fishDish.id)
        let tuesdayDinner = MealSlot(dayOfWeek: 1, mealType: MealType.dinner.rawValue, dishId: nil)
        plan.slots = [mondayDinner, tuesdayDinner]

        let result = AutocompleteEngine.autocomplete(
            emptySlots: [tuesdayDinner],
            currentPlan: plan,
            allDishes: [fishDish],
            allTags: [tag]
        )

        XCTAssertEqual(result.unfilledCount, 1)
        XCTAssertNil(tuesdayDinner.dishId)
    }
}

