namespace MyOffice.Data.Repositories.Account;

using Microsoft.EntityFrameworkCore;
using Models.Accounts;

public class MotionRepository : AppRepository<Motion>, IMotionRepository
{
	public bool Add(Motion motion)
	{
		return AddBase(motion) > 0;
	}

	public List<Motion> GetByAccount(Guid accountId, DateTime dateFrom, DateTime dateTo)
	{
		return _context
			.Motions
			.Include(x => x.Item)
			.ThenInclude(x => x.ItemGlobal)
			.Where(x => x.AccountId == accountId && x.DateTime >= dateFrom && x.DateTime <= dateTo)
			.OrderByDescending(x => x.DateTime)
			.ThenByDescending(x => x.CreatedOn)
			.ToList();
	}

	public Motion? Get(Guid userId, Guid id)
	{
		return _context
			.Motions
			.Include(x => x.Item)
			.ThenInclude(x => x.ItemGlobal)
			.FirstOrDefault(x => x.Account!.AccessRights!.Any(a => a.UserId == userId) && x.Id == id);
	}

	public bool Update(Motion motion)
	{
		return UpdateBase(motion) > 0;
	}

	public bool Remove(Motion motion)
	{
		return RemoveBase(motion) > 0;
	}
}