namespace MyOffice.Data.Repositories.Item;

using Microsoft.EntityFrameworkCore;
using MyOffice.Data.Models.Items;
using MyOffice.DbContext;

public class ItemCategoryRepository : AppRepository<ItemCategory>, IItemCategoryRepository
{
	public ItemCategoryRepository(AppDbContext context) : base(context)
	{
	}

	public List<ItemCategory> GetAll(Guid userId)
	{
		return _context.ItemCategories
			.Include(x => x.Items)
			.Where(x => x.UserId == userId)
			.ToList();
	}

	public async Task<List<ItemCategory>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default)
	{
		return await _context.ItemCategories
			.Include(x => x.Items)
			.Where(x => x.UserId == userId)
			.ToListAsync(cancellationToken);
	}

	public bool Add(ItemCategory itemCategory)
	{
		return AddBase(itemCategory) > 0;
	}

	public async Task<bool> AddAsync(ItemCategory itemCategory, CancellationToken cancellationToken = default)
	{
		return await AddBaseAsync(itemCategory, cancellationToken) > 0;
	}

	public ItemCategory? Get(Guid userId, Guid id)
	{
		return _context.ItemCategories
			.Include(x => x.Items)
			.FirstOrDefault(x => x.Id == id && x.UserId == userId);
	}

	public async Task<ItemCategory?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default)
	{
		return await _context.ItemCategories
			.Include(x => x.Items)
			.FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, cancellationToken);
	}

	public bool Update(ItemCategory itemCategory)
	{
		return UpdateBase(itemCategory) > 0;
	}

	public async Task<bool> UpdateAsync(ItemCategory itemCategory, CancellationToken cancellationToken = default)
	{
		return await UpdateBaseAsync(itemCategory, cancellationToken) > 0;
	}

	public bool Remove(ItemCategory itemCategory)
	{
		return RemoveBase(itemCategory) > 0;
	}

	public async Task<bool> RemoveAsync(ItemCategory itemCategory, CancellationToken cancellationToken = default)
	{
		return await RemoveBaseAsync(itemCategory, cancellationToken) > 0;
	}
}
