using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Models;
namespace Sledgemapper.Api.Data
{
    public static class DbInitializer
    {
        public static void Initialize(MyDbContext context)
        {
            context.Database.EnsureCreated();
        }
    }

    public class MyDbContext : DbContext
    {
        public DbSet<MapLog> MapLogs { get; set; }
        public DbSet<Session> Sessions { get; set; }
        public DbSet<UserConnection> UserConnections { get; set; }
        public DbSet<SessionUser> SessionUsers { get; set; }
        public DbSet<Snapshot> Snapshots { get; set; }

        public MyDbContext(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=MyDatabase.db").UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

        //     base.OnConfiguring(optionsBuilder);
        // }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 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);
        }
    }
}