namespace MyOffice.Web.Controllers;

using AutoMapper;
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;
using Microsoft.AspNetCore.Mvc;

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

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

	public ObjectResult AccountCategories()
	{
		var list = _accountService.GetAllCategories(UserId);

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

	public ObjectResult AccountCategory([AsGuid] string id)
	{
		var exec = _accountService.GetCategory(UserId, id.AsGuid());

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

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

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

	public ObjectResult AccountCategoriesAdd(AccountCategoryViewModel request)
	{
		var exec = _accountService.CategoryAdd(UserId, new AccountCategoryDto { Name = request.Name! });
		switch (exec.Status)
		{
			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<AccountCategoryViewModel>(exec.Result!));

			case GeneralExecStatus.failure:
			case GeneralExecStatus.not_found:
				return ProblemBadResponse("Adding account category failed.");

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

	public ObjectResult AccountCategoriesUpdate([AsGuid] string id, AccountCategoryViewModel request)
	{
		var exec = _accountService.CategoryUpdate(UserId, id.AsGuid(), new AccountCategoryDto { Name = request.Name! });
		switch (exec.Status)
		{
			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<AccountCategoryViewModel>(exec.Result!));

			case GeneralExecStatus.failure:
			case GeneralExecStatus.not_found:
				return ProblemBadResponse("Update account category failed.");

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

	public ObjectResult AccountCategoriesDelete([AsGuid] string id)
	{
		var exec = _accountService.CategoryRemove(UserId, id.AsGuid());
		switch (exec.Status)
		{
			case AccountCategoryRemoveResult.success:
				return OkResponse(_mapper.Map<AccountCategoryViewModel>(exec.Result!));

			case AccountCategoryRemoveResult.failure:
				return ProblemBadResponse("Remove account category failed.");
			case AccountCategoryRemoveResult.not_found:
				return ProblemBadResponse("Account category not found.");
			case AccountCategoryRemoveResult.accounts_exists:
				return ProblemBadResponse("Account category have accounts.");

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