using System.Security.Cryptography.X509Certificates;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TIAM.Entities.Users;
using TIAM.Entities.Products;

namespace TIAM.Database.ModelBuilders.Products;

public static class ProductEntityTypeBuilderExtensions
{
    #region Product

    public static void BuildProductToUserProductMappingRelation(this EntityTypeBuilder<Product> modelBuilder, bool autoInclude = true)
    {
        modelBuilder
            .HasMany(e => e.Users)
            .WithMany(e => e.Products)
            .UsingEntity<UserProductMapping>();
        
        //modelBuilder
        //    .HasMany(e => e.UserProductMappings)
        //    .WithOne(e => e.Product).HasForeignKey(x => x.ProductId);

        modelBuilder.Navigation(e => e.UserProductMappings).AutoInclude(autoInclude);
        modelBuilder.Navigation(e => e.Users).AutoInclude(autoInclude);
    }

    public static void BuildProductToServiceProviderRelation(this EntityTypeBuilder<Product> modelBuilder, bool autoInclude = true)
    {
        modelBuilder
            .HasOne(x => x.ServiceProvider)
            .WithMany(x => x.Products)
            .HasForeignKey(x => x.ServiceProviderId);

        modelBuilder.Navigation(e => e.ServiceProvider).AutoInclude(autoInclude);
    }

    public static void BuildProductToProfileRelation(this EntityTypeBuilder<Product> modelBuilder, bool autoInclude = true)
    {
        //modelBuilder.HasOne(x => x.Profile)

        modelBuilder.Navigation(e => e.Profile).AutoInclude(autoInclude);
    }

    #endregion Product
}