namespace MyOffice.Services.Account.Domain;

using AutoMapper;
using Data.Models.Accounts;
using MyOffice.Services.Identity;

public class AccountAccessDto
{
	public Guid AccountId { get; set; }
	public AccountDto? Account { get; set; }

	/// <summary>
	/// User can access to account
	/// </summary>
	public Guid UserId { get; set; }
	public UserDto? User { get; set; }

	public bool IsAllowRead { get; set; }
	public bool IsAllowWrite { get; set; }
	public bool IsAllowManage { get; set; }
	public bool IsOwner { get; set; }
	public AccountAccessTypeEnum Type { get; set; }
	/// <summary>
	/// Who add access
	/// </summary>
	public Guid OwnerId { get; set; }
	public UserDto? Owner { get; set; }
	public string? Name { get; set; }
}

public class AccountAccessDtoProfile : Profile
{
	public AccountAccessDtoProfile()
	{
		CreateMap<AccountAccess, AccountAccessDto>()
			.AfterMap<AccountAccessDtoMappingAction>()
		;
	}
}

public class AccountAccessDtoMappingAction : IMappingAction<AccountAccess, AccountAccessDto>
{
	private readonly IContextProvider _contextProvider;
	public AccountAccessDtoMappingAction(IContextProvider contextProvider)
	{
		_contextProvider = contextProvider;
	}

	public void Process(AccountAccess source, AccountAccessDto destination, ResolutionContext context)
	{
		destination.IsOwner = destination.OwnerId == _contextProvider.UserId;
	}
}