namespace MyOffice.Data.Repositories.Currency;

using System.Xml.Schema;
using Microsoft.EntityFrameworkCore;
using Models.Currencies;
using MyOffice.DbContext;

public class CurrencyRateRepository : AppRepository<CurrencyRate>, ICurrencyRateRepository
{
	public CurrencyRateRepository(AppDbContext context) : base(context)
	{
	}

	public List<CurrencyRate> GetLastRates(Guid currencyId, DateTime? before = null, int count = 1)
	{
		before = before ?? DateTime.UtcNow;

		return _context.CurrencyRates
			.Where(x => x.CurrencyId == currencyId)
			.Where(x => x.DateTime <= before)
			.OrderByDescending(x => x.DateTime)
			.ThenByDescending(x => x.Id)
			.Take(count)
			.ToList();
	}

	public async Task<List<CurrencyRate>> GetLastRatesAsync(
		Guid currencyId,
		DateTime? before = null,
		int count = 1,
		CancellationToken cancellationToken = default)
	{
		before ??= DateTime.UtcNow;

		return await _context.CurrencyRates
			.Where(x => x.CurrencyId == currencyId)
			.Where(x => x.DateTime <= before)
			.OrderByDescending(x => x.DateTime)
			.ThenByDescending(x => x.Id)
			.Take(count)
			.ToListAsync(cancellationToken);
	}

	public Dictionary<string, CurrencyRate> GetLastRates(Guid userId, List<string> currencyIds, DateTime? before = null)
	{
		return _context.CurrencyRates
			.Include(x => x.Currency)
			.Include(x => x.Currency!.CurrencyGlobal)
			.Where(x => x.DateTime <= before)
			.Where(x => x.Currency!.UserId == userId)
			.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
			.GroupBy(x => new
			{
				x.Currency!.CurrencyGlobalId
			}, (key, g) => new
			{
				key.CurrencyGlobalId,
				rate = g.OrderByDescending(x => x.DateTime).First()
			})
			.ToDictionary(x => x.CurrencyGlobalId, x => x.rate);
	}

	public async Task<Dictionary<string, CurrencyRate>> GetLastRatesAsync(
		Guid userId,
		List<string> currencyIds,
		DateTime? before = null,
		CancellationToken cancellationToken = default
	)
	{
		var rows = await _context.CurrencyRates
			.Include(x => x.Currency)
			.Include(x => x.Currency!.CurrencyGlobal)
			.Where(x => x.DateTime <= before)
			.Where(x => x.Currency!.UserId == userId)
			.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
			.GroupBy(x => new
			{
				x.Currency!.CurrencyGlobalId
			}, (key, g) => new
			{
				key.CurrencyGlobalId,
				rate = g.OrderByDescending(x => x.DateTime).First()
			})
			.ToListAsync(cancellationToken);

		return rows.ToDictionary(x => x.CurrencyGlobalId, x => x.rate);
	}

	public bool AddRate(CurrencyRate currencyRate)
	{
		return this.AddBase(currencyRate) > 0;
	}

	public async Task<bool> AddRateAsync(CurrencyRate currencyRate, CancellationToken cancellationToken = default)
	{
		return await AddBaseAsync(currencyRate, cancellationToken) > 0;
	}

	public List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date)
	{
		return _context.CurrencyRates
			.Where(x => x.CurrencyId == currencyId && x.DateTime == date)
			.ToList();
	}

	public async Task<List<CurrencyRate>> GetAtDateAsync(
		Guid currencyId,
		DateTime date,
		CancellationToken cancellationToken = default)
	{
		return await _context.CurrencyRates
			.Where(x => x.CurrencyId == currencyId && x.DateTime == date)
			.ToListAsync(cancellationToken);
	}
}