using AyCode.Interfaces.Users;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Numerics;
using AyCode.Interfaces.Profiles;
using TIAM.Entities.Products;
using TIAM.Entities.Transfers;
using TIAM.Entities.Users;

namespace TIAM.Database.ModelBuilders.Users;

public static class UserEntityTypeBuilderExtensions
{
    #region User

    public static void BuildUserToUserProductMappingRelation(this EntityTypeBuilder<User> modelBuilder, bool autoInclude = true)
    {
        modelBuilder
            .HasMany(e => e.Products)
            .WithMany(e => e.Users)
            .UsingEntity<UserProductMapping>();
                //l => l.HasOne<Product>().WithMany(x=>x.UserProductMappings).HasForeignKey(e => e.ProductId),
                //r => r.HasOne<User>().WithMany(x=>x.UserProductMappings).HasForeignKey(e => e.UserId));

        //modelBuilder
        //    .HasMany(e => e.UserProductMappings)
        //    .WithOne(e => e.User).HasForeignKey(x => x.UserId);
        //    //.WithMany(e => e.User).
        //    //.UsingEntity<UserProductMapping>.HasForeignKey(x => x.UserId);

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

    #endregion User

    #region UserProductMapping
    /// <summary>
    /// Mapping User and Product relations
    /// </summary>
    public static void BuildUserProductMappingToRelations(this EntityTypeBuilder<UserProductMapping> modelBuilder, bool autoInclude)
    {
        modelBuilder.BuildUserProductMappingToUserRelation(autoInclude);
        modelBuilder.BuildUserProductMappingToProductRelation(autoInclude);
    }

    /// <summary>
    /// Mapping User relation
    /// </summary>
    public static void BuildUserProductMappingToUserRelation(this EntityTypeBuilder<UserProductMapping> modelBuilder, bool autoInclude)
    {
        modelBuilder
            .HasOne(e => e.User)
            .WithMany(e => e.UserProductMappings).HasForeignKey(x => x.UserId);

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

    /// <summary>
    /// Mapping Product relation
    /// </summary>
    public static void BuildUserProductMappingToProductRelation(this EntityTypeBuilder<UserProductMapping> modelBuilder, bool autoInclude)
    {
        modelBuilder
            .HasOne(e => e.Product)
            .WithMany(e => e.UserProductMappings).HasForeignKey(x => x.ProductId);

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

    /// <summary>
    /// Mapping User relation
    /// </summary>
    public static void BuildTransferToUserProductMappingRelation(this EntityTypeBuilder<UserProductMapping> modelBuilder, bool autoInclude)
    {
        modelBuilder
            .HasMany(e => e.Transfers)
            .WithOne(e => e.UserProductMapping).HasForeignKey(x => x.UserProductMappingId);

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

    #endregion UserProductMapping
}