using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Core.Entities;
using Sledgemapper.Api.Models;
using Sledgemapper.Entities;

namespace Sledgemapper.Api.Infrastructure.Data
{
    // public static class DbInitializer
    // {
    //     public static void Initialize(SledgemapperDbContext context)
    //     {
    //         context.Database.EnsureCreated();
    //     }
    // }

    public class SledgemapperDbContext : IdentityDbContext
    {
        public DbSet<Sledgemapper.Api.Core.Entities.Campaign> Campaigns { get; set; }
        public DbSet<MapLog> MapLogs { get; set; }
        public DbSet<Map> Maps { get; set; }
        public DbSet<Session> Sessions { get; set; }
        public DbSet<SessionUser> SessionUsers { get; set; }
        public DbSet<Snapshot> Snapshots { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<UserConnection> UserConnections { get; set; }

        public SledgemapperDbContext(DbContextOptions options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //  optionsBuilder.
            //  options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
            //  optionsBuilder.UseSqlite("Filename=SledgemapperDatabase.db", options =>
            //  {
            //      options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
            //  });

            optionsBuilder.UseSqlite("Filename=Sledgemapper.db").UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Core.Entities.Campaign>().HasOne<User>(e=>e.Owner);
 
modelBuilder.Entity<Core.Entities.Campaign>().HasMany<User>(e=>e.InvitedUsers).WithMany(e=>e.Campaigns);

  modelBuilder.Entity<User>() //Use your application user class here
           .ToTable( "Users" ); //Set the table name here

            // // Map table names
            // modelBuilder.Entity<MapLog>().ToTable("MapLog", "dbo");
            // modelBuilder.Entity<MapLog>(entity =>
            // {
            //     entity.HasKey(e => e.MapLogId);
            // });

            // modelBuilder.Entity<Session>().ToTable("Session", "dbo");
            // modelBuilder.Entity<Session>(entity =>
            // {
            //     entity.HasKey(e => e.SessionId);
            //     entity.HasIndex(e => e.SessionName).IsUnique();
            // });


            // modelBuilder.Entity<UserConnection>().ToTable("UserConnection", "dbo");
            // modelBuilder.Entity<UserConnection>(entity =>
            // {
            //     entity.HasKey(e => e.UserConnectionId);
            //     entity.HasIndex(e => e.UserId);

            // });

            // modelBuilder.Entity<SessionUser>().ToTable("SessionUser", "dbo");
            // modelBuilder.Entity<SessionUser>(entity =>
            // {
            //     entity.HasKey(e => e.SessionUserId);
            //     entity.HasIndex(e => e.SessionId);

            // });

            //   modelBuilder.Entity<Snapshot>().ToTable("Snapshot", "dbo");
            // modelBuilder.Entity<Snapshot>(entity =>
            // {
            //     entity.HasKey(e => e.SnapshotId);
            // });

            base.OnModelCreating(modelBuilder);
        }
    }
}