using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TIAM.Entities.Products;

namespace TIAM.Database.DbSets.Products;

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

    public static bool AddProduct(this IProductDbSet ctx, Product product)
        => ctx.Products.Add(product).State == EntityState.Added;

    public static bool UpdateProduct(this IProductDbSet ctx, Product product)
        => ctx.Products.Update(product).State == EntityState.Modified;

    public static bool RemoveProduct(this IProductDbSet ctx, Product product)
        => ctx.Products.Remove(product).State == EntityState.Deleted;

    #endregion Add, Update, Remove

    public static IQueryable<Product> ProductsWithUserRelations(this IProductDbSet ctx, bool autoInclude = true)
        => autoInclude
            ? ctx.Products
                .Include(x => x.UserProductMappings)
                .ThenInclude(x => x.User)
            : ctx.Products;

    public static Product? GetProductById(this IProductDbSet ctx, Guid productId, bool includeUsers = true)
        => ctx.ProductsWithUserRelations(includeUsers).FirstOrDefault(x => x.Id == productId);

    public static IQueryable<Product> GetProductsByServiceProviderId(this IProductDbSet ctx, Guid serviceProviderId, bool includeUsers = true)
        => ctx.ProductsWithUserRelations(includeUsers).Where(x => x.ServiceProviderId == serviceProviderId);

}