using Microsoft.EntityFrameworkCore;
using TIAM.Database.DbSets.Products;
using TIAM.Database.DbSets.Users;
using TIAM.Entities.Products;
using TIAM.Entities.TransferDestinations;
using TIAM.Entities.Transfers;

namespace TIAM.Database.DbSets.Transfers;

public static class TransferDbSetExtensions
{
    #region Add, Update, Remove

    public static bool AddTransfer(this ITransferDbSet ctx, Transfer transfer)
        => ctx.Transfers.Add(transfer).State == EntityState.Added;

    public static bool UpdateTransfer(this ITransferDbSet ctx, Transfer transfer)
        => ctx.Transfers.Update(transfer).State == EntityState.Modified;

    public static bool RemoveTransfer(this ITransferDbSet ctx, Transfer transfer)
        => ctx.Transfers.Remove(transfer).State == EntityState.Deleted;

    public static bool RemoveTransfer(this ITransferDbSet ctx, Guid transferId)
    {
        var transfer = ctx.GetTransferById(transferId, false);
        return transfer == null || ctx.RemoveTransfer(transfer);
    }

    #endregion Add, Update, Remove

    public static Transfer? GetTransferById(this ITransferDbSet ctx, Guid transferId, bool autoInclude = false)
        => ctx.Transfers.FirstOrDefault(x => x.Id == transferId);    

    public static IQueryable<Transfer> GetTransfers(this ITransferDbSet ctx)
        => ctx.Transfers;    

    public static TransferDestination? GetTransferDestinationById(this ITransferDestinationDbSet ctx, Guid transferDestinationId)
        => ctx.TransferDestinations.FirstOrDefault(x => x.Id == transferDestinationId);    

    public static IQueryable<TransferDestination> GetTransferDestinations(this ITransferDestinationDbSet ctx)
        => ctx.TransferDestinations;

    #region TransferDestination
    public static bool AddTransferDestination(this ITransferDestinationDbSet ctx, TransferDestination transferDestination)
        => ctx.TransferDestinations.Add(transferDestination).State == EntityState.Added;

    public static bool UpdateTransferDestination(this ITransferDestinationDbSet ctx, TransferDestination transferDestination)
        => ctx.TransferDestinations.Update(transferDestination).State == EntityState.Modified;

    public static bool RemoveTransferDestination(this ITransferDestinationDbSet ctx, TransferDestination transferDestination)
        => ctx.TransferDestinations.Remove(transferDestination).State == EntityState.Deleted;

    public static bool RemoveTransferDestination(this ITransferDestinationDbSet ctx, Guid transferDestinationId)
    {
        var transfer = ctx.GetTransferDestinationById(transferDestinationId);
        return transfer == null || ctx.RemoveTransferDestination(transferDestinationId);
    }

    #endregion TransferDestination
}