namespace MyOffice.Services.Account;

using MyOffice.Core;
using MyOffice.Core.Extensions;
using MyOffice.Data.Models.Accounts;
using MyOffice.Services.Account.Domain;

public partial class AccountService
{
	public List<AccountDto> GetAllAccounts(Guid userId)
	{
		return _accountRepository
			.GetAll(userId)
			.ToDto<AccountDto>(_mapper);
	}

	public List<AccountDto> GetByCategory(Guid userId, Guid categoryId)
	{
		return _accountRepository
			.GetByCategory(userId, categoryId)
			.ToDto<AccountDto>(_mapper);
	}

	public List<AccountDto> FindAccounts(Guid userId, string term)
	{
		return _accountRepository
			.FindAccounts(userId, term)
			.ToDto<AccountDto>(_mapper);
	}

	public List<AccountDetailedDto> GetByCategoryDetailed(Guid userId, Guid categoryId)
	{
		return _accountRepository
			.GetByCategoryDetailed(userId, categoryId)
			.ToDto<AccountDetailedDto>(_mapper);
	}

	public Exec<AccountDetailedDto, GeneralExecStatus> GetByIdDetailed(Guid userId, Guid id)
	{
		var result = new Exec<AccountDetailedDto, GeneralExecStatus>(GeneralExecStatus.success);

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

		return result.Set(account.ToDto<AccountDetailedDto>(_mapper));;
	}

	public Exec<AccountDto, AccountAddStatus> AccountAdd(Guid userId, AccountAdd input)
	{
		if (input == null)
			throw new ArgumentNullException(nameof(input));

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

		var category = _accountCategoryRepository.Get(userId, input.CategoryId);
		if (category == null)
		{
			return result.Set(AccountAddStatus.category_not_found);
		}
		var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId);
		if (currency == null)
		{
			return result.Set(AccountAddStatus.currency_not_found);
		}

		var account = new Account
		{
			Id = Guid.NewGuid(),
			Name = input.Name,
			CurrencyGlobalId = currency.CurrencyGlobalId,
			OwnerId = userId,
		};

		account.Categories = new List<AccountAccountCategory>
			{
				new()
				{
					AccountId = account.Id,
					CategoryId = category.Id,
				}
			};
		account.AccessRights = new List<AccountAccess>
			{
				new()
				{
					AccountId = account.Id,
					UserId = userId,
					OwnerId = userId,
					IsAllowManage = true,
					IsAllowRead = true,
					IsAllowWrite = true,
				}
			};

		if (!_accountRepository.Add(account))
		{
			return result.Set(AccountAddStatus.failure);
		}
		// TODO: Add category refactoring
		if (!_accountAccountCategoryRepository.Add(account.Categories.FirstOrDefault()!))
		{
			return result.Set(AccountAddStatus.failure);
		}
		// TODO: Add access refactoring
		if (!_accountAccessRepository.Add(account.AccessRights.FirstOrDefault()!))
		{
			return result.Set(AccountAddStatus.failure);
		}

		// TODO: Add account update AccessRights ???
		var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
		if (access != null && access.Type.ToString() != input.Type)
		{
			if (Enum.TryParse<AccountAccessTypeEnum>(input.Type, out var enumType))
			{
				access.Type = enumType;
				_accountAccessRepository.Update(access);
			}
		}

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

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

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

		if (account.Motions!.Any())
		{
			return result.Set(GeneralExecStatus.not_found);
		}

		_accountRepository.Delete(account);
		result.Set(account.ToDto<AccountDto>(_mapper));

		return result;
	}

	public Exec<AccountDto, AccountEditStatus> AccountUpdate(Guid userId, Guid accountId, AccountEdit input)	
	{
		if (input == null)
			throw new ArgumentNullException(nameof(input));

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

		AccountCategory? category = null;
		if (input.CategoryId.HasValue)
		{
			category = _accountCategoryRepository.Get(userId, input.CategoryId.Value);
			if (category == null)
			{
				return result.Set(AccountEditStatus.category_not_found);
			}
		}

		var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId);
		if (currency == null)
		{
			return result.Set(AccountEditStatus.currency_not_found);
		}

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

		account.Name = input.Name;
		if (account.CurrencyGlobalId != currency.CurrencyGlobalId)
		{
			account.CurrencyGlobalId = currency.CurrencyGlobalId;
		}

		if (category != null && account.Categories!.All(x => x.CategoryId != category.Id))
		{
			_accountAccountCategoryRepository.Add(new AccountAccountCategory
			{
				AccountId = account.Id,
				CategoryId = category.Id,
			});
		}

		if (!_accountRepository.Update(account))
		{
			return result.Set(AccountEditStatus.failure);
		}

		var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
		if (access != null && access.Type.ToString() != input.Type)
		{
			if (Enum.TryParse<AccountAccessTypeEnum>(input.Type, out var enumType))
			{
				access.Type = enumType;
				_accountAccessRepository.Update(access);
			}
		}

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