using CanteenDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;

namespace CanteenDatabaseImplement
{
    public class CanteenDatabase : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (optionsBuilder.IsConfigured == false)
            {
                optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-AOSK1F5\SQLEXPRESS;Initial Catalog=CanteenDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
            }
            base.OnConfiguring(optionsBuilder);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DishProduct>()
                .HasKey(dp => new { dp.DishId, dp.ProductId });

            modelBuilder.Entity<DishProduct>()
                .HasOne(dp => dp.Dish)
                .WithMany(d => d.Products)
                .HasForeignKey(dp => dp.DishId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<DishProduct>()
                .HasOne(dp => dp.Product)
                .WithMany(p => p.Dishes)
                .HasForeignKey(dp => dp.ProductId)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================
            modelBuilder.Entity<LunchOrder>()
                .HasKey(dp => new { dp.LunchId, dp.OrderId });

            modelBuilder.Entity<LunchOrder>()
                .HasOne(dp => dp.Lunch)
                .WithMany(d => d.Orders)
                .HasForeignKey(dp => dp.LunchId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<LunchOrder>()
                .HasOne(dp => dp.Order)
                .WithMany(p => p.Lunches)
                .HasForeignKey(dp => dp.OrderId)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================
            modelBuilder.Entity<LunchProduct>()
                .HasKey(dp => new { dp.LunchId, dp.ProductId });

            modelBuilder.Entity<LunchProduct>()
                .HasOne(dp => dp.Lunch)
                .WithMany(d => d.Products)
                .HasForeignKey(dp => dp.LunchId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<LunchProduct>()
                .HasOne(dp => dp.Product)
                .WithMany(p => p.Lunches)
                .HasForeignKey(dp => dp.ProductId)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================
            modelBuilder.Entity<OrderCook>()
                .HasKey(dp => new { dp.OrderId, dp.CookId });

            modelBuilder.Entity<OrderCook>()
                .HasOne(dp => dp.Order)
                .WithMany(d => d.Cooks)
                .HasForeignKey(dp => dp.OrderId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<OrderCook>()
                .HasOne(dp => dp.Cook)
                .WithMany(p => p.Orders)
                .HasForeignKey(dp => dp.CookId)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================
            modelBuilder.Entity<OrderDish>()
                .HasKey(dp => new { dp.OrderId, dp.DishId });

            modelBuilder.Entity<OrderDish>()
                .HasOne(dp => dp.Order)
                .WithMany(d => d.Dishes)
                .HasForeignKey(dp => dp.OrderId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<OrderDish>()
                .HasOne(dp => dp.Dish)
                .WithMany(p => p.Orders)
                .HasForeignKey(dp => dp.DishId)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================
            modelBuilder.Entity<ProductCook>()
                .HasKey(dp => new { dp.ProductId, dp.CookId });

            modelBuilder.Entity<ProductCook>()
                .HasOne(dp => dp.Product)
                .WithMany(d => d.Cooks)
                .HasForeignKey(dp => dp.ProductId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<ProductCook>()
                .HasOne(dp => dp.Cook)
                .WithMany(p => p.Products)
                .HasForeignKey(dp => dp.CookId)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================
            modelBuilder.Entity<Order>()
                .HasOne(b => b.Visitor)
                .WithMany(a => a.Orders)
                .OnDelete(DeleteBehavior.NoAction);
            //=====================================

        }

        public virtual DbSet<Cook> Cooks { set; get; }
        public virtual DbSet<OrderCook> OrderCook { set; get; }
        public virtual DbSet<Dish> Dishes { set; get; }
        public virtual DbSet<Lunch> Lunches { set; get; }
        public virtual DbSet<LunchOrder> LunchOrder { set; get; }
        public virtual DbSet<Manager> Managers { set; get; }
        public virtual DbSet<Order> Orders { set; get; }
        public virtual DbSet<Product> Products { set; get; }
        public virtual DbSet<ProductCook> ProductCook { set; get; }
        public virtual DbSet<LunchProduct> LunchProduct { set; get; }
        public virtual DbSet<OrderDish> OrderDish { set; get; }
        public virtual DbSet<DishProduct> DishProduct { set; get; }
        public virtual DbSet<Tableware> Tablewares { set; get; }
        public virtual DbSet<Visitor> Visitors { set; get; }
    }
}
