using Microsoft.EntityFrameworkCore;
using moduleExamen.Models;

namespace moduleExamen.Data;

public class SportShopContext : DbContext
{
    private static readonly string ConnectionString =
        "server=91.222.238.6;username=rover_user;password=root123;database=Dem_dem";

    public DbSet<Role> Roles { get; set; }
    public DbSet<Status> Statuses { get; set; }
    public DbSet<Ed> Eds { get; set; }
    public DbSet<Manufact> Manufacts { get; set; }
    public DbSet<Suplier> Supliers { get; set; }
    public DbSet<TipeProduct> TipeProducts { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderItem> OrderItems { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql(ConnectionString, ServerVersion.AutoDetect(ConnectionString));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Role>(entity =>
        {
            entity.ToTable("Role");
            entity.HasKey(e => e.IdRole);
            entity.Property(e => e.IdRole).HasColumnName("idRole");
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(45);
        });

        modelBuilder.Entity<Status>(entity =>
        {
            entity.ToTable("Status");
            entity.HasKey(e => e.IdStatus);
            entity.Property(e => e.IdStatus).HasColumnName("idStatus");
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(45);
        });

        modelBuilder.Entity<Ed>(entity =>
        {
            entity.ToTable("ed");
            entity.HasKey(e => e.Ided);
            entity.Property(e => e.Ided).HasColumnName("ided");
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(45);
        });

        modelBuilder.Entity<Manufact>(entity =>
        {
            entity.ToTable("manufact");
            entity.HasKey(e => e.Idmanufact);
            entity.Property(e => e.Idmanufact).HasColumnName("idmanufact");
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(45);
        });

        modelBuilder.Entity<Suplier>(entity =>
        {
            entity.ToTable("suplier");
            entity.HasKey(e => e.Idsuplier);
            entity.Property(e => e.Idsuplier).HasColumnName("idsuplier");
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(45);
        });

        modelBuilder.Entity<TipeProduct>(entity =>
        {
            entity.ToTable("tipe_product");
            entity.HasKey(e => e.IdtipeProduct);
            entity.Property(e => e.IdtipeProduct).HasColumnName("idtipe_product");
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(45);
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.ToTable("user");
            entity.HasKey(e => e.Iduser);
            entity.Property(e => e.Iduser).HasColumnName("iduser");
            entity.Property(e => e.Login).HasColumnName("login").HasMaxLength(45);
            entity.Property(e => e.Password).HasColumnName("password").HasMaxLength(45);
            entity.Property(e => e.Fio).HasColumnName("fio").HasMaxLength(45);
            entity.Property(e => e.IdRole).HasColumnName("id_role");
            entity.Property(e => e.Email).HasColumnName("email").HasMaxLength(45);

            entity.HasOne(d => d.IdRoleNavigation)
                .WithMany(p => p.Users)
                .HasForeignKey(d => d.IdRole)
                .HasConstraintName("id_role");
        });

        modelBuilder.Entity<Product>(entity =>
        {
            entity.ToTable("product");
            entity.HasKey(e => e.Idproduct);
            entity.Property(e => e.Idproduct).HasColumnName("idproduct");
            entity.Property(e => e.Articl).HasColumnName("articl").HasMaxLength(45);
            entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(100);
            entity.Property(e => e.IdTipeProduct).HasColumnName("id_tipe_product");
            entity.Property(e => e.ProductContent).HasColumnName("product_content").HasMaxLength(150);
            entity.Property(e => e.IdManufactur).HasColumnName("id_manufactur");
            entity.Property(e => e.IdSuplier).HasColumnName("id_suplier");
            entity.Property(e => e.PriceProduct).HasColumnName("price_product").HasColumnType("decimal(10,2)");
            entity.Property(e => e.Sale).HasColumnName("sale");
            entity.Property(e => e.KolVo).HasColumnName("kol-vo");
            entity.Property(e => e.IdEd).HasColumnName("id_ed");
            entity.Property(e => e.IdPhoto).HasColumnName("id_photo");

            entity.HasOne(d => d.IdEdNavigation)
                .WithMany(p => p.Products)
                .HasForeignKey(d => d.IdEd)
                .HasConstraintName("id_ed");

            entity.HasOne(d => d.IdManufacturNavigation)
                .WithMany(p => p.Products)
                .HasForeignKey(d => d.IdManufactur)
                .HasConstraintName("id_manufact");

            entity.HasOne(d => d.IdSuplierNavigation)
                .WithMany(p => p.Products)
                .HasForeignKey(d => d.IdSuplier)
                .HasConstraintName("id_supplier");

            entity.HasOne(d => d.IdTipeProductNavigation)
                .WithMany(p => p.Products)
                .HasForeignKey(d => d.IdTipeProduct)
                .HasConstraintName("id_tipe_product");
        });

        modelBuilder.Entity<Order>(entity =>
        {
            entity.ToTable("Orders");
            entity.HasKey(e => e.Idzakaz);
            entity.Property(e => e.Idzakaz).HasColumnName("idzakaz");
            entity.Property(e => e.Article).HasColumnName("article").HasMaxLength(45);
            entity.Property(e => e.Fio).HasColumnName("fio");
            entity.Property(e => e.IdStatus).HasColumnName("id_Status");
            entity.Property(e => e.Adrec).HasColumnName("adrec").HasMaxLength(100);
            entity.Property(e => e.DataStart).HasColumnName("data_start");
            entity.Property(e => e.DateEnd).HasColumnName("date_end");

            entity.HasOne(d => d.FioNavigation)
                .WithMany(p => p.Orders)
                .HasForeignKey(d => d.Fio)
                .HasConstraintName("foi");

            entity.HasOne(d => d.IdStatusNavigation)
                .WithMany(p => p.Orders)
                .HasForeignKey(d => d.IdStatus)
                .HasConstraintName("id_status");
        });

        modelBuilder.Entity<OrderItem>(entity =>
        {
            entity.ToTable("order_items");
            entity.HasKey(e => e.IdorderItems);
            entity.Property(e => e.IdorderItems).HasColumnName("idorder_items");
            entity.Property(e => e.IdOrder).HasColumnName("id_order");
            entity.Property(e => e.IdProduct).HasColumnName("id_product");
            entity.Property(e => e.KolVo).HasColumnName("kol-vo");

            entity.HasOne(d => d.IdOrderNavigation)
                .WithMany(p => p.OrderItems)
                .HasForeignKey(d => d.IdOrder)
                .HasConstraintName("id_order");

            entity.HasOne(d => d.IdProductNavigation)
                .WithMany(p => p.OrderItems)
                .HasForeignKey(d => d.IdProduct)
                .HasConstraintName("id_product");
        });
    }
}
