using Microsoft.EntityFrameworkCore;
namespace CocktailWeb.Data
{
    public class DbDataContext : DbContext
    {

        protected readonly IConfiguration _configuration;

        public DbSet<Flasche> Flaschen { get; set; }
        public DbSet<CocktailFlasche> CocktailFlaschen { get; set; }
        public DbSet<Cocktail> Cocktails { get; set; }
        public DbSet<Filler> Fillers { get; set; }

        public DbSet<Glas> Glaeser { get; set; }

        public DbDataContext(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite(_configuration.GetConnectionString("CocktailDB"));
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Flasche>().ToTable("Flaschen");
            modelBuilder.Entity<Flasche>().HasData(
                new Flasche("Jägermeister") { Id = 1, Alkoholisch = true },
                new Flasche("Wodka") { Id = 2, Alkoholisch = true }
                );

            modelBuilder.Entity<Filler>().HasData(
                new Filler() { Id = 1, Pos = 1, Type = Filler.FillerType.Pump },
                new Filler() { Id = 2, Pos = 2, Type = Filler.FillerType.Pump },
                new Filler() { Id = 3, Pos = 3, Type = Filler.FillerType.Pump },
                new Filler() { Id = 4, Pos = 1, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 5, Pos = 2, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 6, Pos = 3, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 7, Pos = 4, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 8, Pos = 5, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 9, Pos = 6, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 10, Pos = 7, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 11, Pos = 8, Type = Filler.FillerType.Pourer },
                new Filler() { Id = 12, Pos = 4, Type = Filler.FillerType.Pump },
                new Filler() { Id = 13, Pos = 5, Type = Filler.FillerType.Pump },
                new Filler() { Id = 14, Pos = 6, Type = Filler.FillerType.Pump }
                );
            modelBuilder.Entity<Filler>().HasIndex(p => new {p.Type, p.Pos}).IsUnique();


            modelBuilder.Entity<Glas>().ToTable("Glaeser");

            //    modelBuilder.Entity<Cocktail>().HasMany(c => c.CocktailFlaschen).WithOne(c => c.Cocktail).HasForeignKey(c => c.CocktailID);


        }
    }
}
