using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace songrequests_blazor.Data
{
    public class LocalDbContext : IdentityDbContext<IdentityUser>
    {

        public DbSet<Song> Songs { get; set; }
        public DbSet<Session> Sessions { get; set; }
        public DbSet<QueuedSong> QueuedSongs { get; set; }
        public DbSet<Library> Libraries { get; set; }

        public LocalDbContext(DbContextOptions<LocalDbContext> options) : base(options)
        { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        { }

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

            //    // Foreign Keys und dazugehörige Indizes (aus anderem Beispiel, daher andere Klassen- und Spaltennamen)
            //    entity.HasIndex(e => e.MAC_ID, "FK_MAC_ID_idx");
            //    entity.HasOne(d => d.Machine).WithMany(p => p.Logs).HasForeignKey(d => d.MAC_ID).HasConstraintName("FK_log_machine");

            modelBuilder.Entity<Library>(entity =>
            {
                entity.HasMany(l => l.Songs).WithOne(s => s.Library).HasForeignKey(s => s.Library_ID).HasConstraintName("FK_library_songs");
                //entity.HasMany(l => l.UsedInSessions).WithMany(s => s.EnabledLibraries).UsingEntity("Sessions_Libraries");
            });

            modelBuilder.Entity<QueuedSong>(entity =>
            {
                entity.HasOne(qs => qs.Song).WithMany(s => s.Queued).HasForeignKey(s => s.Song_ID).HasConstraintName("FK_queuedsongs_song");
                entity.HasMany(qs => qs.UserVotes).WithMany(u => u.VotedSongs).UsingEntity("QueuedSongs_UserVotes");
                entity.HasOne(qs => qs.AdddedBy).WithMany(u => u.AddedSongs).HasForeignKey(qs => qs.AddedBy_User_ID).HasConstraintName("FK_user_addedsongs");
            });

            modelBuilder.Entity<Session>(entity =>
            {
                entity.HasMany(ses => ses.EnabledLibraries).WithMany(l => l.UsedInSessions).UsingEntity("Sessions_Libraries");
                entity.HasMany(ses => ses.QueuedSongs).WithOne(qs => qs.Session).HasForeignKey(qs => qs.Session_ID).HasConstraintName("FK_Session_QueuedSongs");

            });


            string AdminRoleGUID = "fc985a25-19ce-48ed-8443-c845f550960a";
            string AdminUserGUID = "5c2511d0-6a08-4950-babd-d107996749ba";

            modelBuilder.Entity<IdentityRole>().HasData(
                new IdentityRole { Id = AdminRoleGUID, Name = "Administrator", NormalizedName = "ADMINISTRATOR".ToUpper() }
                );

            var hasher = new PasswordHasher<IdentityUser>();
            var AdminUser = new IdentityUser()
            {
                Id = AdminUserGUID, // primary key
                UserName = "admin@admin.com",
                NormalizedUserName = "ADMIN@ADMIN.COM",
                Email = "admin@admin.com",
                NormalizedEmail = "ADMIN@ADMIN.COM"
            };
            AdminUser.PasswordHash = hasher.HashPassword(AdminUser, "admin");

            modelBuilder.Entity<IdentityUser>().HasData(AdminUser);

            modelBuilder.Entity<IdentityUserRole<string>>().HasData(
                 new IdentityUserRole<string>
                 {
                     RoleId = AdminRoleGUID,
                     UserId = AdminUserGUID
                 }
             );


            // Erweitertes definieren des Datenbankmodells (falls benötigt):
            //modelBuilder.Entity<Song>(entity =>
            //{
            //    // Festlegen, wie die Tabelle der Entity heissen soll
            //    // hier im Beispiel aber nicht nötig, da EF Core sowieso die englische Plural verwendet oder es versucht
            //    entity.ToTable("Songs");

            //    // Tabelle mit Basisdaten füllen
            //    entity.HasData(
            //        new Song() { Id = 1, Title = "Llama Song", Artist = "Winamp  Allstars" }
            //     );

            //    // Unique-Index definieren
            //    entity.HasIndex(p => new { p.Title, p.Artist }).IsUnique();

            //    // Properties für Spalten definieren
            //    entity.Property(e => e.Title)
            //        .HasMaxLength(255);

            //    // Columntype (manuell) festlegen
            //    entity.Property(e => e.Source)
            //        .HasColumnType("enum('LocalFile','Spotify')");

            //    // Foreign Keys und dazugehörige Indizes (aus anderem Beispiel, daher andere Klassen- und Spaltennamen)
            //    entity.HasIndex(e => e.MAC_ID, "FK_MAC_ID_idx");
            //    entity.HasOne(d => d.Machine).WithMany(p => p.Logs).HasForeignKey(d => d.MAC_ID).HasConstraintName("FK_log_machine");
            //});

        }
    }
}
