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

namespace CartWise.Domain.Tests;

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

    [Fact]
    public void Constructor_CreatesOwnerMembershipForCreator()
    {
        var household = new Household("The Smiths", "user-1", Now);

        var member = Assert.Single(household.Members);
        Assert.Equal("user-1", member.UserId);
        Assert.Equal(HouseholdRole.Owner, member.Role);
        Assert.Equal(household.HouseholdId, member.HouseholdId);
        Assert.Equal(Now, member.JoinedUtc);
    }

    [Fact]
    public void Constructor_TrimsName()
    {
        var household = new Household("  The Smiths  ", "user-1", Now);

        Assert.Equal("The Smiths", household.Name);
    }

    [Theory]
    [InlineData("")]
    [InlineData("   ")]
    [InlineData(null)]
    public void Constructor_ThrowsWhenNameIsMissing(string? name)
    {
        Assert.Throws<ArgumentException>(() => new Household(name!, "user-1", Now));
    }

    [Theory]
    [InlineData("")]
    [InlineData("   ")]
    [InlineData(null)]
    public void Constructor_ThrowsWhenCreatedByUserIdIsMissing(string? userId)
    {
        Assert.Throws<ArgumentException>(() => new Household("The Smiths", userId!, Now));
    }

    [Fact]
    public void AddMember_AddsMemberWithGivenRole()
    {
        var household = new Household("The Smiths", "user-1", Now);

        var member = household.AddMember("user-2", HouseholdRole.Member, Now);

        Assert.Equal(2, household.Members.Count);
        Assert.Equal("user-2", member.UserId);
        Assert.Equal(HouseholdRole.Member, member.Role);
    }

    [Fact]
    public void AddMember_ThrowsWhenUserIsAlreadyAMember()
    {
        var household = new Household("The Smiths", "user-1", Now);

        Assert.Throws<InvalidOperationException>(() => household.AddMember("user-1", HouseholdRole.Member, Now));
    }

    [Fact]
    public void IsMember_ReturnsTrueForExistingMemberAndFalseOtherwise()
    {
        var household = new Household("The Smiths", "user-1", Now);

        Assert.True(household.IsMember("user-1"));
        Assert.False(household.IsMember("user-2"));
    }
}
