namespace MyOffice.Services.Account;

using System.Collections.Generic;

using Core;
using Core.Extensions;
using Data.Models.Accounts;
using Domain;

public partial class AccountService
{
	public Exec<AccountAccessInviteDto, AccessInviteStatus> AccessInvite(Guid userId, Guid accountId, string email, bool isAllowWrite)
	{
		if (email == null)
			throw new ArgumentNullException(nameof(email));

		var result = new Exec<AccountAccessInviteDto, AccessInviteStatus>(AccessInviteStatus.success);

		var account = _accountRepository.Get(userId, accountId);
		if (account == null)
		{
			return result.Set(AccessInviteStatus.account_not_found);
		}

		if (account.AccessRights!.Any(x => x.User!.Email.EqualsIgnoreCase(email)))
		{
			return result.Set(AccessInviteStatus.access_exists);
		}

		var access = _accountAccessInviteRepository.Get(userId, email);
		if (access != null)
		{
			return result.Set(AccessInviteStatus.invite_exists);
		}

		var invite = new AccountAccessInvite
		{
			Id = Guid.NewGuid(),
			CreatedOn = DateTime.UtcNow,
			UserId = userId,
			AccountId = account.Id,
			Email = email.SafeTrim()!,
			IsAllowWrite = isAllowWrite,
		};

		_accountAccessInviteRepository.Add(invite);

		return result.Set(_mapper.Map<AccountAccessInviteDto>(invite));
	}

	public Exec<AccountDto, GeneralExecStatus> AccessUpdate(Guid userId, Guid accountId, List<AccountAccessDto> accesses)
	{
		if (accesses == null)
			throw new ArgumentNullException(nameof(accesses));

		var result = new Exec<AccountDto, GeneralExecStatus>(GeneralExecStatus.success);

		var account = _accountRepository.Get(userId, accountId);
		if (account == null)
		{
			return result.Set(GeneralExecStatus.not_found);
		}

		foreach (var access in account.AccessRights!)
		{
			var newAccess = accesses.FirstOrDefault(x => x.UserId == access.UserId);
			if (newAccess != null && newAccess.IsAllowWrite != access.IsAllowWrite)
			{
				access.IsAllowWrite = newAccess.IsAllowWrite;
				_accountAccessRepository.Update(access);
			}
		}

		account = _accountRepository.Get(userId, accountId);

		return result.Set(_mapper.Map<AccountDto>(account));
	}

	public Exec<AccountDto, GeneralExecStatus> AccessDelete(Guid userId, Guid accountId, Guid accessUserId)
	{
		var result = new Exec<AccountDto, GeneralExecStatus>(GeneralExecStatus.success);

		var account = _accountRepository.Get(userId, accountId);
		if (account == null || !account.AccessRights!.Any() || account.AccessRights!.Count() == 1)
		{
			return result.Set(GeneralExecStatus.not_found);
		}

		var access = account.AccessRights!.FirstOrDefault(x => x.UserId == accessUserId);
		if (access == null)
		{
			return result.Set(GeneralExecStatus.not_found);
		}
		if (access.OwnerId == access.UserId)
		{
			return result.Set(GeneralExecStatus.not_found);
		}

		_accountAccessRepository.Delete(access);

		account = _accountRepository.Get(userId, accountId);

		return result.Set(_mapper.Map<AccountDto>(account));

	}

	public List<AccountAccessInviteDto> InvitesGet(string email)
	{
		return _mapper.Map<List<AccountAccessInviteDto>>(_accountAccessInviteRepository.GetActive(email).ToList());
	}

	public Exec<AccountDto, InviteAcceptStatus> InviteAccept(Guid userId, Guid id, string name)
	{
		var result = new Exec<AccountDto, InviteAcceptStatus>(InviteAcceptStatus.success);

		var invite = _accountAccessInviteRepository.Get(id);
		if (invite == null
			|| !invite.Email.EqualsIgnoreCase(_contextProvider.User.Email)
			|| invite.AcceptedOn.HasValue
			|| invite.RejectedOn.HasValue
		)
		{
			return result.Set(InviteAcceptStatus.invite_not_found);
		}

		var account = _accountRepository.Get(invite.UserId, invite.AccountId);
		if (account == null)
		{
			return result.Set(InviteAcceptStatus.account_not_found);
		}

		if (account.AccessRights!.Any(x => x.UserId == userId))
		{
			result.Set(InviteAcceptStatus.already_accepted);
		}
		else
		{
			_accountAccessRepository.Add(new AccountAccess
			{
				UserId = userId,
				AccountId = account.Id,
				OwnerId = invite.UserId,
				Name = name,
				IsAllowWrite = invite.IsAllowWrite,
				Type = AccountAccessTypeEnum.external,
			});
		}

		invite.AcceptedOn = DateTime.UtcNow;
		_accountAccessInviteRepository.Update(invite);

		account = _accountRepository.Get(userId, account.Id);

		return result.Set(_mapper.Map<AccountDto>(account));
	}

	public Exec<AccountAccessInviteDto, GeneralExecStatus> InviteReject(Guid id)
	{
		var result = new Exec<AccountAccessInviteDto, GeneralExecStatus>(GeneralExecStatus.success);

		var invite = _accountAccessInviteRepository.Get(id);
		if (invite == null
			|| !invite.Email.EqualsIgnoreCase(_contextProvider.User.Email)
			|| invite.AcceptedOn.HasValue
			|| invite.RejectedOn.HasValue
		)
		{
			return result.Set(GeneralExecStatus.not_found);
		}

		invite.RejectedOn = DateTime.UtcNow;
		_accountAccessInviteRepository.Update(invite);

		return result.Set(_mapper.Map<AccountAccessInviteDto>(invite));
	}
}
