using AyCode.Interfaces.Profiles;
using AyCode.Interfaces.Users;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Formats.Tar;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Profiles.Dtos;
using AyCode.Interfaces.ServiceProviders;
using Microsoft.EntityFrameworkCore;

namespace AyCode.Database.ModelBuilders.Users;

public static class AcUserEntityTypeBuilderExtensions
{
    public static void BuildEntityToProfileRelation<TProfileRelation, TProfile>(this EntityTypeBuilder<TProfileRelation> modelBuilder, bool autoInclude = true) 
        where TProfileRelation : class, IAcProfileRelation<TProfile> 
        where TProfile : class, IAcProfileDtoBase
    {
        modelBuilder.HasOne(x => x.Profile).WithOne().OnDelete(DeleteBehavior.Cascade);
        modelBuilder.Navigation(e => e.Profile).AutoInclude(autoInclude).IsRequired();
    }

    public static void BuildEntityToServiceProviderRelation<TCompanyRelation, TCompany, TUserToServiceProvider, TProfile>(this EntityTypeBuilder<TCompanyRelation> modelBuilder, bool autoInclude = true) 
        where TCompanyRelation : class, IAcUserBase, IAcCompanyRelation<TCompany, TUserToServiceProvider> 
        where TCompany : class, IAcCompany<TCompanyRelation, TUserToServiceProvider, TProfile>
        where TUserToServiceProvider : class, IAcUserToCompanyBase
        where TProfile : class, IAcProfileDtoBase
    {
        modelBuilder
            .HasMany(e => e.ServiceProviders)
            .WithMany(e => e.Users)
            .UsingEntity<TUserToServiceProvider>();
        //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.ServiceProviders).AutoInclude(autoInclude);
        modelBuilder.Navigation(e => e.UserToServiceProviders).AutoInclude(autoInclude);
    }
}