namespace MyOffice.Web.Controllers;

using AutoMapper;
using Core;
using Core.Extensions;
using Infrastructure.Attributes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Models.Currency;
using Services.Currency;
using Services.Currency.Domain;

[Authorize]
[DefaultFromBody]
public class SettingCurrencyController : BaseApiController
{
	private readonly CurrencyService _currencyService;
	private readonly ILogger<SettingCurrencyController> _logger;
	private readonly IMapper _mapper;

	public SettingCurrencyController(
		CurrencyService currencyService,
		ILogger<SettingCurrencyController> logger,
		IMapper mapper
	)
	{
		_currencyService = currencyService;
		_logger = logger;
		_mapper = mapper;
	}

	public ObjectResult Get()
	{
		var list = _currencyService.GetAllWithRates(UserId);

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

	public ObjectResult Add(CurrencyAddModel currency)
	{
		var exec = _currencyService.CurrencyAdd(UserId, new CurrencyDto
		{
			UserId = UserId,
			CurrencyGlobalId = currency.Id,
			Name = currency.Name,
			ShortName = currency.ShortName,
		});

		switch (exec.Status)
		{
			case CurrencyAddStatus.success:
			case CurrencyAddStatus.exists:
				_currencyService.CurrencyRateAdd(UserId, exec.Result!.Id, new CurrencyRateDto
				{
					CurrencyId = exec.Result!.Id,
					Rate = currency.Rate,
					Quantity = currency.Quantity,
					DateTime = currency.RateDate.Date,
				});
				return OkResponse(_mapper.Map<CurrencyViewModel>(exec.Result!));

			case CurrencyAddStatus.failed:
				return ProblemBadResponse("Adding currency failed.");

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

	public ObjectResult Update(string id, CurrencyEditModel currency)
	{
		var exec = _currencyService.CurrencyUpdate(
			UserId,
			id.AsGuid(),
			new CurrencyEdit { Name = currency.Name, ShortName = currency.ShortName, IsPrimary = currency.IsPrimary }
		);

		switch (exec.Status)
		{
			case CurrencyEditStatus.success:
				return OkResponse(_mapper.Map<CurrencyViewModel>(exec.Result!));

			case CurrencyEditStatus.not_found:
			case CurrencyEditStatus.failed:
				return ProblemBadResponse("Currency not found.");

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

	public ObjectResult Delete([AsGuid] string id)
	{
		var exec = _currencyService.Remove(UserId, id.AsGuid());

		switch (exec.Status)
		{
			case GeneralExecStatus.success:
				return OkResponse(_mapper.Map<CurrencyViewModel>(exec.Result!));

			case GeneralExecStatus.not_found:
			case GeneralExecStatus.failure:
				return ProblemBadResponse("Currency not found.");

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

	public ObjectResult AddRate(string id, CurrencyRateModel currencyRate)
	{
		var exec = _currencyService.CurrencyRateAdd(UserId, id.AsGuid(), new CurrencyRateDto
		{
			CurrencyId = id.AsGuid(),
			Quantity = currencyRate.Quantity,
			Rate = currencyRate.Rate,
			DateTime = currencyRate.RateDate.Date,
		});

		switch (exec.Status)
		{
			case CurrencyAddRateStatus.failed:
				return ProblemBadResponse("Adding currency rate failed.");
			case CurrencyAddRateStatus.not_found:
				return ProblemBadResponse("Currency not found.");

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

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