namespace MyOffice.Services.Dashboard;

using MyOffice.Services.Dashboard.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Core.Extensions;
using Data.Repositories.Account;
using Data.Repositories.Currency;
using MyOffice.Data.Models.Accounts;
using System.Xml.Linq;

public class DashboardService
{
	private readonly IAccountRepository _accountRepository;
	private readonly ICurrencyRateRepository _currencyRateRepository;

	public DashboardService(
		IAccountRepository accountRepository,
		ICurrencyRateRepository currencyRateRepository
	)
	{
		_accountRepository = accountRepository;
		_currencyRateRepository = currencyRateRepository;
	}

	public DashboardData GetDashboardRestData(Guid userId)
	{
		var rests = _accountRepository.GetRestAtDate(userId, DateTime.UtcNow);

		var result = new DashboardData
		{
			Balance = rests
				.Where(x => (x.Type == AccountAccessTypeEnum.balance || x.Type == AccountAccessTypeEnum.credit) && x.CurrencyRate.HasValue)
				.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),

			BalanceDebit = rests
				.Where(x => x.Type == AccountAccessTypeEnum.balance && x.CurrencyRate.HasValue)
				.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),

			BalanceCredit = rests
				.Where(x => x.Type == AccountAccessTypeEnum.credit && x.CurrencyRate.HasValue)
				.Sum(x => x.Balance * (x.CurrencyRate * x.CurrencyQuantity)),

			BalanceRests = rests
				.Where(x => x.Type == AccountAccessTypeEnum.balance)
				.Where(x => x.CurrencyRate.HasValue && x.Balance.HasValue && x.Balance != 0)
				.Select(x => new DashboardRestData
				{
					Id = x.Id,
					Name = x.Name,
					Balance = x.Balance,
					CurrencyName = x.CurrencyName!,
					CurrencyShortName = x.CurrencyShortName!,
					CurrencyRate = x.CurrencyRate,
					CurrencyQuantity = x.CurrencyQuantity,
				})
				.OrderByDescending(x => x.BalanceAtRate)
				.ToList(),
		};

		return result;
	}

	public DashboardIncomeData GetDashboardIncomeData(Guid userId, DateTime from, DateTime to, Guid? category)
	{
		var data = category.HasValue
			? _accountRepository.GetIncomeByCategory(userId, category.Value, from.ToUtc(), to.ToUtc())
			: _accountRepository.GetIncomeByCategories(userId, from.ToUtc(), to.ToUtc());

		data = data
			.Where(x => x.Amount != 0)
			.ToList();

		var currencies = data
			.Select(x => x.CurrencyId)
			.Distinct()
			.ToList();

		var rates = _currencyRateRepository
			.GetLastRates(userId, currencies, to.ToUtc())
			.ToDictionary(x => x.Key, x => x.Value.Rate * x.Value.Quantity);

		return new DashboardIncomeData
		{
			Data = data.Select(x => new
			{
				Id = x.Id.ToShort(),
				Name = x.Name,
				ValueRaw = x.Amount,
				Value = (x.Amount * rates.FirstOrDefault(r => r.Key == x.CurrencyId).Value),
			})
			.GroupBy(x => new
			{
				x.Id,
				x.Name,
			})
			.Select(x => new DashboardIncomeDataItem
			{
				Id = x.Key.Id,
				Name = x.Key.Name,
				Value = x.Sum(s => s.Value),
				ValueRaw = x.Sum(s => s.ValueRaw),
			})
			.OrderBy(x => x.Name)
			.ToList(),

			Details = data.Select(x => new
			{
				Id = x.CurrencyId,
				Name = x.CurrencyName,
				ValueRaw = x.Amount,
				Value = (x.Amount * rates.FirstOrDefault(r => r.Key == x.CurrencyId).Value),
			})
			.GroupBy(x => new
			{
				x.Id,
				x.Name,
			})
			.Select(x => new DashboardIncomeDataItem
			{
				Id = x.Key.Id,
				Name = x.Key.Name,
				Value = x.Sum(s => s.Value),
				ValueRaw = x.Sum(s => s.ValueRaw),
			})
			.OrderBy(x => x.Name)
			.ToList(),
		};
	}

	public DashboardIncomeData GetDashboardOutcomeData(Guid userId, DateTime from, DateTime to, Guid? category)
	{
		var data = category.HasValue
			? _accountRepository.GetOutcomeByCategory(userId, category.Value, from.ToUtc(), to.ToUtc())
			: _accountRepository.GetOutcomeByCategories(userId, from.ToUtc(), to.ToUtc());

		data = data
			.Where(x => x.Amount != 0)
			.ToList();

		var currencies = data
			.Select(x => x.CurrencyId)
			.Distinct()
			.ToList();

		var rates = _currencyRateRepository
			.GetLastRates(userId, currencies, to.ToUtc())
			.ToDictionary(x => x.Key, x => x.Value.Rate * x.Value.Quantity);

		return new DashboardIncomeData
		{
			Data = data.Select(x => new
				{
					Id = x.Id.ToShort(),
					Name = x.Name,
					ValueRaw = x.Amount,
					Value = (x.Amount * rates.FirstOrDefault(r => r.Key == x.CurrencyId).Value),
				})
				.GroupBy(x => new
				{
					x.Id,
					x.Name,
				})
				.Select(x => new DashboardIncomeDataItem
				{
					Id = x.Key.Id,
					Name = x.Key.Name,
					Value = x.Sum(s => s.Value),
					ValueRaw = x.Sum(s => s.ValueRaw),
				})
				.OrderBy(x => x.Name)
				.ToList(),

			Details = data.Select(x => new
				{
					Id = x.CurrencyId,
					Name = x.CurrencyName,
					ValueRaw = x.Amount,
					Value = (x.Amount * rates.FirstOrDefault(r => r.Key == x.CurrencyId).Value),
				})
				.GroupBy(x => new
				{
					x.Id,
					x.Name,
				})
				.Select(x => new DashboardIncomeDataItem
				{
					Id = x.Key.Id,
					Name = x.Key.Name,
					Value = x.Sum(s => s.Value),
					ValueRaw = x.Sum(s => s.ValueRaw),
				})
				.OrderBy(x => x.Name)
				.ToList(),
		};
	}
}
