namespace MyOffice.Web.Controllers;

using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using Core;
using Core.Extensions;
using Models.Account;
using Services.Account;
using Services.Account.Domain;
using Infrastructure.Attributes;
using Services.Identity;

[Authorize]
[DefaultFromBody]
public class SettingsAccountController : BaseApiController
{
	private readonly ILogger<SettingsAccountController> _logger;
	private readonly IMapper _mapper;
	private readonly AccountService _accountService;
	private readonly IContextProvider _contextProvider;

	public SettingsAccountController(
		ILogger<SettingsAccountController> logger,
		IMapper mapper,
		AccountService accountService,
		IContextProvider contextProvider
	)
	{
		_logger = logger;
		_mapper = mapper;
		_accountService = accountService;
		_contextProvider = contextProvider;
	}

	public ObjectResult AccountsGet([AsGuid(true)] string? category)
	{
		var list = category.IsPresent()
			? _accountService.GetByCategory(UserId, category!.AsGuid())
			: _accountService.GetAllAccounts(UserId);

		return OkResponse(_mapper.Map<List<AccountViewModel>>(list.OrderBy(x => x.Name)));
	}

	public ObjectResult AccountsAdd(AccountViewModel request)
	{
		var exec = _accountService.AccountAdd(UserId, new AccountAdd
		{
			Name = request.Name,
			CurrencyId = request.CurrencyId,
			CategoryId = request.CategoryId.AsGuid(),
			Type = request.Type,
		});

		switch (exec.Status)
		{
			case AccountAddStatus.category_not_found:
				return ProblemBadResponse("Category not found.");
			case AccountAddStatus.currency_not_found:
				return ProblemBadResponse("Currency not found.");
			case AccountAddStatus.failure:
				return ProblemBadResponse("Adding account failed.");
			case AccountAddStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountsUpdate([AsGuid] string id, AccountEditRequestModel request)
	{
		var exec = _accountService.AccountUpdate(UserId, id.AsGuid(), new AccountEdit
		{
			Name = request.Name,
			CurrencyId = request.CurrencyId,
			CategoryId = request.CategoryId?.AsGuidNull(),
			UserId = request.UserId?.AsGuidNull(),
			Type = request.Type,
		});

		switch (exec.Status)
		{
			case AccountEditStatus.not_found:
				return ProblemBadResponse("Account not found.");
			case AccountEditStatus.category_not_found:
				return ProblemBadResponse("Category not found.");
			case AccountEditStatus.currency_not_found:
				return ProblemBadResponse("Currency not found.");
			case AccountEditStatus.failure:
				return ProblemBadResponse("Adding account failed.");
			case AccountEditStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountsDelete([AsGuid] string id)
	{
		var exec = _accountService.AccountDelete(UserId, id);
		switch (exec.Status)
		{
			case GeneralExecStatus.not_found:
				return ProblemBadResponse("Account not found.");
			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountsCategoryDelete([AsGuid] string id, [AsGuid] string categoryId)
	{
		var exec = _accountService.AccountCategoryRemove(UserId, id.AsGuid(), categoryId.AsGuid());

		switch (exec.Status)
		{
			case GeneralExecStatus.not_found:
			case GeneralExecStatus.failure:
				return ProblemBadResponse("Category not found.");

			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountsAccessAdd([AsGuid] string id, AccountAccessViewModel request)
	{
		var model = _mapper.Map<List<AccountAccessDto>>(request.Accesses);

		var exec = _accountService.AccessUpdate(UserId, id.AsGuid(), model);

		switch (exec.Status)
		{
			case GeneralExecStatus.not_found:
			case GeneralExecStatus.failure:
				return ProblemBadResponse("Account not found.");

			case GeneralExecStatus.success:
				break;

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}

		if (!request.Email.IsPresent())
		{
			return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));
		}

		var inviteExec = _accountService.AccessInvite(UserId, id.AsGuid(), request.Email!, request.AllowWrite);

		switch (inviteExec.Status)
		{
			case AccessInviteStatus.account_not_found:
				return ProblemBadResponse("Account not found.");

			case AccessInviteStatus.access_exists:
			case AccessInviteStatus.invite_exists:
			case AccessInviteStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountsAccessDelete([AsGuid] string id, [AsGuid] string userId)
	{
		var exec = _accountService.AccessDelete(UserId, id.AsGuid(), userId.AsGuid());

		switch (exec.Status)
		{
			case GeneralExecStatus.not_found:
			case GeneralExecStatus.failure:
				return ProblemBadResponse("Account not found.");

			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountInvites()
	{
		var invites = _accountService.InvitesGet(_contextProvider.User.Email);

		return OkResponse(_mapper.Map<List<AccountAccessInviteViewModel>>(invites));
	}

	public ObjectResult AccountInviteAccept([AsGuid] string id, AccountInviteAcceptRequest request)
	{
		var exec = _accountService.InviteAccept(UserId, id.AsGuid(), request.Name);

		switch (exec.Status)
		{
			case InviteAcceptStatus.invite_not_found:
				return ProblemBadResponse("Invite not found.");
			case InviteAcceptStatus.account_not_found:
				return ProblemBadResponse("Account not found.");

			case InviteAcceptStatus.already_accepted:
			case InviteAcceptStatus.success:
				return OkResponse(_mapper.Map<AccountViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}

	public ObjectResult AccountInviteReject([AsGuid] string id)
	{
		var exec = _accountService.InviteReject(id.AsGuid());

		switch (exec.Status)
		{
			case GeneralExecStatus.not_found:
			case GeneralExecStatus.failure:
				return ProblemBadResponse("Invite not found.");

			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<AccountAccessInviteViewModel>(exec.Result!));

			default:
				throw new NotSupportedException(exec.Status.ToString());
		}
	}
}