namespace MyOffice.Data.Repositories.Account;

using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.Data.Repositories;

public class AccountAccessInviteRepository : AppRepository<AccountAccessInvite>, IAccountAccessInviteRepository
{
	public AccountAccessInvite? Get(Guid userId, string email)
	{
		return _context.AccountAccessInvites
			.FirstOrDefault(x => x.UserId == userId
							 && x.Email == email
							 && !x.AcceptedOn.HasValue
							 && !x.RejectedOn.HasValue
							 );
	}

	public bool Add(AccountAccessInvite invite)
	{
		return AddBase(invite) > 0;
	}

	public bool Update(AccountAccessInvite invite)
	{
		return UpdateBase(invite) > 0;
	}

	public IEnumerable<AccountAccessInvite> GetActive(string email)
	{
		return _context
			.AccountAccessInvites
			.Include(x => x.Account)
			.Where(x => x.Email == email && !x.AcceptedOn.HasValue && !x.RejectedOn.HasValue);

	}

	public AccountAccessInvite? Get(Guid id)
	{
		return _context.AccountAccessInvites.FirstOrDefault(x => x.Id == id);
	}
}