namespace MyOffice.Web.Controllers;

using AutoMapper;
using Core.Extensions;
using Infrastructure.Attributes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyOffice.Web.Models.Dashboard;
using Services.Dashboard;

[Authorize]
[ApiController]
[Route("api/dashboard")]
[DefaultFromBody]
public class DashboardController : BaseApiController
{
	private readonly ILogger<DashboardController> _logger;
	private readonly DashboardService _dashboardService;
	private readonly IMapper _mapper;

	public DashboardController(
		ILogger<DashboardController> logger,
		DashboardService dashboardService,
		IMapper mapper
	)
	{
		_logger = logger;
		_dashboardService = dashboardService;
		_mapper = mapper;
	}

	[HttpGet]
	public async Task<ObjectResult> Index(CancellationToken cancellationToken)
	{
		var data = await _dashboardService.GetDashboardRestDataAsync(UserId, cancellationToken);

		return OkResponse(_mapper.Map<DashboardViewModel>(data));
	}

	[HttpGet("income")]
	public async Task<ObjectResult> Income(
		DateTime from,
		DateTime to,
		[AsGuid(true)] string? category,
		CancellationToken cancellationToken
	)
	{
		var data = await _dashboardService.GetDashboardIncomeDataAsync(
			UserId,
			from.StartOfDay(),
			to.EndOfDay(),
			category?.AsGuidNull(),
			cancellationToken);

		return OkResponse(_mapper.Map<DashboardIncomeDataViewModel>(data));
	}

	[HttpGet("outcome")]
	public async Task<ObjectResult> Outcome(
		DateTime from,
		DateTime to,
		[AsGuid(true)] string? category,
		CancellationToken cancellationToken
	)
	{
		var data = await _dashboardService.GetDashboardOutcomeDataAsync(
			UserId,
			from.StartOfDay(),
			to.EndOfDay(),
			category?.AsGuidNull(),
			cancellationToken);

		return OkResponse(_mapper.Map<DashboardIncomeDataViewModel>(data));
	}
}
