using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AyCode.Database.DbContexts;
using Microsoft.EntityFrameworkCore;
using TIAM.Database.ModelBuilders.Emails;
using TIAM.Database.ModelBuilders.Products;
using TIAM.Database.ModelBuilders.Transfers;
using TIAM.Database.ModelBuilders.Users;
using TIAM.Entities.Addresses;
using TIAM.Entities.Drivers;
using TIAM.Entities.Emails;
using TIAM.Entities.Permissions;
using TIAM.Entities.Products;
using TIAM.Entities.Profiles;
using TIAM.Entities.ServiceProviders;
using TIAM.Entities.TransferDestinations;
using TIAM.Entities.Transfers;
using TIAM.Entities.Users;

namespace TIAM.Database.DbContexts.Admins
{
    public class AdminDbContext : DbContextBase, IAdminDbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<TransferDestination> TransferDestinations { get; set; }
        public DbSet<Transfer> Transfers { get; set; }

        public DbSet<TiamServiceProvider> ServiceProviders { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<UserProductMapping> UserProductMappings { get; set; }
        public DbSet<PermissionGroup> PermissionGroups { get; set; }
        public DbSet<PermissionGroupUserMapping> PermissionGroupUserMappings { get; set; }
        public DbSet<PermissionContextMapping> PermissionContextMappings { get; set; }
        public DbSet<PermissionsType> PermissionsTypes { get; set; }
        
        public DbSet<EmailMessage> EmailMessages { get; set; }

        public DbSet<Address> Addresses { get; set; }

        public AdminDbContext() //: this(string.Empty)
        {
        }

        public AdminDbContext(string name) : base(name)
        {
        }

        public AdminDbContext(DbContextOptions<DbContext> options, string name) : base(options, name)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            new UserEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<User>());
            new ProfileEntityTypeConfigurations().Configure(modelBuilder.Entity<Profile>());
            new UserProductMappingEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<UserProductMapping>());

            new ProductEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<Product>());

            new TransferEntityTypeDefaultConfigurations().Configure(modelBuilder.Entity<Transfer>());
            modelBuilder.Entity<TransferDestination>().Navigation(e => e.Address).AutoInclude(true);

            new EmailMessageEntityTypeDefaultConfigurations().Configure(modelBuilder.Entity<EmailMessage>());

            //modelBuilder.Entity<UserProductToCar>().Ignore(x => x.Id);
            //modelBuilder.Entity<Car>().Ignore(x => x.Id);
            ////modelBuilder.Entity<UserProductToCars>().Ignore(x => x.Cars);
            //modelBuilder.Entity<UserProductMapping>().Ignore(x => x.Cars);

            //modelBuilder.Entity<Car>().HasNoKey();
            //modelBuilder.Entity<UserProductToCar>().HasNoKey();

            modelBuilder.Entity<UserProductMapping>().OwnsOne(
                userToProduct => userToProduct.JsonDetailModel, builderUserProductJsonDetail =>
                {
                    builderUserProductJsonDetail.ToJson();
                    //builderUserProductJsonDetail.Property(nameof(UserProductMapping.JsonDetailModel)).HasColumnName("JsonDetailModel");
                    
                    builderUserProductJsonDetail.OwnsMany(userProductJsonDetail => userProductJsonDetail.Cars, builderUserProductToCar =>
                    {
                        //builderUserProductToCar.ToJson();
                        
                        //builderUserProductToCar.Ignore(x => x.UserProductToCarId);
                        //builderUserProductToCar.Ignore(x => x.Created);
                        //builderUserProductToCar.Ignore(x => x.Modified);

                        builderUserProductToCar.OwnsOne(userProductToCar => userProductToCar.Car, builderCar =>
                        {
                            //builderCar.ToJson();

                            //builderCar.Ignore(x => x.Id);
                            //builderCar.Ignore(x => x.Created);
                            //builderCar.Ignore(x => x.Modified);
                        });
                    });

                    //builderUserProductJsonDetail.OwnsMany(userProductJsonDetail => userProductJsonDetail.Cars2);
                });

            //modelBuilder.Entity<UserProductToCar>().OwnsOne(
            //    userToProduct => userToProduct.Car, ownedNavigationBuilder =>
            //    {
            //        ownedNavigationBuilder.ToJson();
            //    });

        }
    }
}
