using MyOffice.Data.Models.Accounts;
using MyOffice.Services.Account;
using AccountEntity = MyOffice.Data.Models.Accounts.Account;

namespace MyOffice.Tests.Accounts;

public class AccountAclTests
{
	private static AccountEntity CreateAccount(Guid userId, bool write, bool manage)
	{
		return new AccountEntity
		{
			Id = Guid.NewGuid(),
			Name = "Cash",
			AccessRights =
			[
				new AccountAccess
				{
					UserId = userId,
					IsAllowRead = true,
					IsAllowWrite = write,
					IsAllowManage = manage,
				}
			]
		};
	}

	[Fact]
	public void CanWrite_true_when_flag_set()
	{
		var userId = Guid.NewGuid();
		var account = CreateAccount(userId, write: true, manage: false);

		Assert.True(AccountAcl.CanWrite(account, userId));
		Assert.False(AccountAcl.CanManage(account, userId));
	}

	[Fact]
	public void CanManage_true_when_flag_set()
	{
		var userId = Guid.NewGuid();
		var account = CreateAccount(userId, write: false, manage: true);

		Assert.False(AccountAcl.CanWrite(account, userId));
		Assert.True(AccountAcl.CanManage(account, userId));
	}

	[Fact]
	public void CanWrite_false_for_other_user()
	{
		var account = CreateAccount(Guid.NewGuid(), write: true, manage: true);

		Assert.False(AccountAcl.CanWrite(account, Guid.NewGuid()));
		Assert.False(AccountAcl.CanManage(account, Guid.NewGuid()));
	}

	[Fact]
	public void CanWrite_false_when_access_rights_null()
	{
		var account = new AccountEntity { Id = Guid.NewGuid(), Name = "X", AccessRights = null };

		Assert.False(AccountAcl.CanWrite(account, Guid.NewGuid()));
		Assert.False(AccountAcl.CanManage(account, Guid.NewGuid()));
	}
}
