using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using AyCode.Database.DbContexts;
using AyCode.Database.DbContexts.Users;
using AyCode.Database.ModelBuilders.Profiles;
using AyCode.Entities.Users;
using AyCode.Interfaces.Users;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
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.Emails;
using TIAM.Entities.Permissions;
using TIAM.Entities.Products;
using TIAM.Entities.Profiles;
using TIAM.Entities.TransferDestinations;
using TIAM.Entities.Transfers;
using TIAM.Entities.Users;

namespace TIAM.Database.DbContexts.Users
{
    public class UserDbContext : DbContextBase, IUserDbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<UserToken> UserTokens { get; set; }

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

        public DbSet<Transfer> Transfers { get; set; }
        public DbSet<EmailMessage> EmailMessages { get; set; }

        public DbSet<TransferDestination> TransferDestinations { get; set; }

        public UserDbContext() //: this(string.Empty)
        {

        }

        public UserDbContext(DbContextOptions<UserDbContext> options) //: this(string.Empty)
        {

        }

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

        public UserDbContext(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 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<Product>().BuildProductToServiceProviderRelation();
            //new UserProductMappingEntityTypeDefaultConfiguration().Configure(modelBuilder.Entity<UserProductMapping>());

            modelBuilder.Entity<UserProductMapping>().OwnsOne(
                userToProduct => userToProduct.JsonDetailModel, builderUserProductJsonDetail =>
                {
                    builderUserProductJsonDetail.ToJson();
                    
                    builderUserProductJsonDetail.OwnsMany(userProductJsonDetail => userProductJsonDetail.Cars, builderUserProductToCar =>
                    {
                        builderUserProductToCar.OwnsOne(userProductToCar => userProductToCar.Car, builderCar =>
                        {
                        });
                    });

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