using Microsoft.EntityFrameworkCore;
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);
        return transfer == null || ctx.RemoveTransfer(transfer);
    }

    #endregion Add, Update, Remove

    public static Transfer? GetTransferById(this ITransferDbSet ctx, Guid transferId)
        => 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, bool removeAddress)
    {
        if (removeAddress) ctx.Addresses.Remove(transferDestination.Address);

        return ctx.TransferDestinations.Remove(transferDestination).State == EntityState.Deleted;
    }

    public static bool RemoveTransferDestination(this ITransferDestinationDbSet ctx, Guid transferDestinationId, bool removeAddress)
    {
        var transferDestination = ctx.GetTransferDestinationById(transferDestinationId);
        return transferDestination == null || ctx.RemoveTransferDestination(transferDestination, removeAddress);
    }
    #endregion TransferDestination

    #region TransferDestinationToProduct
    public static TransferDestinationToProduct? GetTransferDestinationToProductById(this ITransferDestinationToProductDbSet ctx, Guid transferDestinationToProductId)
        => ctx.TransferDestinationToProducts.FirstOrDefault(x => x.Id == transferDestinationToProductId);    

    public static TransferDestinationToProduct? GetTransferDestinationToProduct(this ITransferDestinationToProductDbSet ctx, Guid productId, Guid transferDestinationId)
        => ctx.TransferDestinationToProducts.FirstOrDefault(x => x.ProductId == productId && x.TransferDestinationId == transferDestinationId);    

    public static bool AddTransferDestinationToProduct(this ITransferDestinationToProductDbSet ctx, TransferDestinationToProduct transferDestinationToProduct)
        => ctx.TransferDestinationToProducts.Add(transferDestinationToProduct).State == EntityState.Added;

    public static bool UpdateTransferDestinationToProduct(this ITransferDestinationToProductDbSet ctx, TransferDestinationToProduct transferDestinationToProduct)
        => ctx.TransferDestinationToProducts.Update(transferDestinationToProduct).State == EntityState.Modified;

    public static bool RemoveTransferDestinationToProduct(this ITransferDestinationToProductDbSet ctx, TransferDestinationToProduct transferDestinationToProduct)
    {
        return ctx.TransferDestinationToProducts.Remove(transferDestinationToProduct).State == EntityState.Deleted;
    }

    public static bool RemoveTransferDestinationToProduct(this ITransferDestinationToProductDbSet ctx, Guid transferDestinationToProductId)
    {
        var transferDestinationToProduct = ctx.GetTransferDestinationToProductById(transferDestinationToProductId);
        return transferDestinationToProduct == null || ctx.RemoveTransferDestinationToProduct(transferDestinationToProduct);
    }
    #endregion TransferDestinationToProduct
}