using CartWise.Domain.Entities;
using CartWise.Domain.Enums;

namespace CartWise.Domain.Tests;

public class ShoppingListItemTests
{
    private static readonly DateTime Now = new(2026, 8, 10, 12, 0, 0, DateTimeKind.Utc);

    [Fact]
    public void TogglePurchased_MarksNeededItemAsPurchased()
    {
        var list = new ShoppingList(Guid.NewGuid(), "Groceries", "user-1", Now);
        var item = list.AddItem("Milk", null, null, "user-1", Now);
        var originalRowVersion = item.RowVersion;

        item.TogglePurchased(Now);

        Assert.Equal(ShoppingListItemStatus.Purchased, item.Status);
        Assert.Equal(Now, item.PurchasedUtc);
        Assert.NotEqual(originalRowVersion, item.RowVersion);
    }

    [Fact]
    public void TogglePurchased_UndoesPurchaseWhenAlreadyPurchased()
    {
        var list = new ShoppingList(Guid.NewGuid(), "Groceries", "user-1", Now);
        var item = list.AddItem("Milk", null, null, "user-1", Now);
        item.TogglePurchased(Now);

        item.TogglePurchased(Now.AddMinutes(5));

        Assert.Equal(ShoppingListItemStatus.Needed, item.Status);
        Assert.Null(item.PurchasedUtc);
    }

    [Fact]
    public void ToggleSkipped_MarksNeededItemAsSkipped()
    {
        var list = new ShoppingList(Guid.NewGuid(), "Groceries", "user-1", Now);
        var item = list.AddItem("Milk", null, null, "user-1", Now);
        var originalRowVersion = item.RowVersion;

        item.ToggleSkipped();

        Assert.Equal(ShoppingListItemStatus.Skipped, item.Status);
        Assert.NotEqual(originalRowVersion, item.RowVersion);
    }

    [Fact]
    public void ToggleSkipped_UnskipsWhenAlreadySkipped()
    {
        var list = new ShoppingList(Guid.NewGuid(), "Groceries", "user-1", Now);
        var item = list.AddItem("Milk", null, null, "user-1", Now);
        item.ToggleSkipped();

        item.ToggleSkipped();

        Assert.Equal(ShoppingListItemStatus.Needed, item.Status);
    }
}
