using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disable

namespace MS.WebApi.AuthServiceDatabase_Models
{
    public partial class AuthServiceDatabase_NewThu04130311PMContext : DbContext
    {
        public AuthServiceDatabase_NewThu04130311PMContext()
        {
        }

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

        public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
        public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
        public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
        public virtual DbSet<AspNetUserRoles> AspNetUserRoles { get; set; }
        public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
        public virtual DbSet<BusinessUnits> BusinessUnits { get; set; }
        public virtual DbSet<IdentityUserBusiUnitM2m> IdentityUserBusiUnitM2m { get; set; }
        public virtual DbSet<MigrationHistory> MigrationHistory { get; set; }
        public virtual DbSet<RoleOperations> RoleOperations { get; set; }
        public virtual DbSet<SiteDevices> SiteDevices { get; set; }
        public virtual DbSet<Sources> Sources { get; set; }
        public virtual DbSet<UserSubIdentities> UserSubIdentities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseNpgsql("Server=ipos.biz;port=5432;database=AuthServiceDatabase_NewThu04130311PM;User Id=postgres;Password=Postgres3141");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasPostgresExtension("uuid-ossp");

            modelBuilder.Entity<AspNetRoles>(entity =>
            {
                entity.HasIndex(e => e.Name)
                    .HasName("AspNetRoles_RoleNameIndex")
                    .IsUnique();

                entity.HasIndex(e => e.ParentRoleId)
                    .HasName("AspNetRoles_IX_ParentRoleId");

                entity.Property(e => e.Id).HasMaxLength(128);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(256);

                entity.Property(e => e.ParentRoleId).HasMaxLength(128);

                entity.HasOne(d => d.ParentRole)
                    .WithMany(p => p.InverseParentRole)
                    .HasForeignKey(d => d.ParentRoleId)
                    .HasConstraintName("FK_public.AspNetRoles_public.AspNetRoles_ParentRoleId");
            });

            modelBuilder.Entity<AspNetUserClaims>(entity =>
            {
                entity.HasIndex(e => e.UserId)
                    .HasName("AspNetUserClaims_IX_UserId");

                entity.Property(e => e.UserId)
                    .IsRequired()
                    .HasMaxLength(128);

                entity.HasOne(d => d.User)
                    .WithMany(p => p.AspNetUserClaims)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK_public.AspNetUserClaims_public.AspNetUsers_UserId");
            });

            modelBuilder.Entity<AspNetUserLogins>(entity =>
            {
                entity.HasKey(e => new { e.LoginProvider, e.ProviderKey, e.UserId })
                    .HasName("PK_public.AspNetUserLogins");

                entity.HasIndex(e => e.UserId)
                    .HasName("AspNetUserLogins_IX_UserId");

                entity.Property(e => e.LoginProvider).HasMaxLength(128);

                entity.Property(e => e.ProviderKey).HasMaxLength(128);

                entity.Property(e => e.UserId).HasMaxLength(128);

                entity.HasOne(d => d.User)
                    .WithMany(p => p.AspNetUserLogins)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK_public.AspNetUserLogins_public.AspNetUsers_UserId");
            });

            modelBuilder.Entity<AspNetUserRoles>(entity =>
            {
                entity.HasKey(e => new { e.UserId, e.RoleId })
                    .HasName("PK_public.AspNetUserRoles");

                entity.HasIndex(e => e.RoleId)
                    .HasName("AspNetUserRoles_IX_RoleId");

                entity.HasIndex(e => e.UserId)
                    .HasName("AspNetUserRoles_IX_UserId");

                entity.Property(e => e.UserId).HasMaxLength(128);

                entity.Property(e => e.RoleId).HasMaxLength(128);

                entity.HasOne(d => d.Role)
                    .WithMany(p => p.AspNetUserRoles)
                    .HasForeignKey(d => d.RoleId)
                    .HasConstraintName("FK_public.AspNetUserRoles_public.AspNetRoles_RoleId");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.AspNetUserRoles)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK_public.AspNetUserRoles_public.AspNetUsers_UserId");
            });

            modelBuilder.Entity<AspNetUsers>(entity =>
            {
                entity.HasIndex(e => e.UserName)
                    .HasName("AspNetUsers_UserNameIndex")
                    .IsUnique();

                entity.Property(e => e.Id).HasMaxLength(128);

                entity.Property(e => e.Alias).HasMaxLength(50);

                entity.Property(e => e.Email).HasMaxLength(256);

                entity.Property(e => e.UserName)
                    .IsRequired()
                    .HasMaxLength(256);
            });

            modelBuilder.Entity<BusinessUnits>(entity =>
            {
                entity.HasIndex(e => e.Name)
                    .HasName("BusinessUnits_IX_Name")
                    .IsUnique();

                entity.HasIndex(e => e.ParentBusinessUnitId)
                    .HasName("BusinessUnits_IX_ParentBusinessUnitId");

                entity.Property(e => e.Id).HasDefaultValueSql("uuid_generate_v4()");

                entity.Property(e => e.Name).HasMaxLength(50);

                entity.HasOne(d => d.ParentBusinessUnit)
                    .WithMany(p => p.InverseParentBusinessUnit)
                    .HasForeignKey(d => d.ParentBusinessUnitId)
                    .HasConstraintName("FK_public.BusinessUnits_public.BusinessUnits_ParentBusinessUnit");
            });

            modelBuilder.Entity<IdentityUserBusiUnitM2m>(entity =>
            {
                entity.HasKey(e => new { e.ServiceIdentityUserId, e.BusinessUnitId })
                    .HasName("PK_public.IdentityUserBusiUnit_M2M");

                entity.ToTable("IdentityUserBusiUnit_M2M");

                entity.HasIndex(e => e.BusinessUnitId)
                    .HasName("IdentityUserBusiUnit_M2M_IX_BusinessUnitId");

                entity.HasIndex(e => e.ServiceIdentityUserId)
                    .HasName("IdentityUserBusiUnit_M2M_IX_ServiceIdentityUserId");

                entity.Property(e => e.ServiceIdentityUserId).HasMaxLength(128);

                entity.HasOne(d => d.BusinessUnit)
                    .WithMany(p => p.IdentityUserBusiUnitM2m)
                    .HasForeignKey(d => d.BusinessUnitId)
                    .HasConstraintName("FK_public.IdentityUserBusiUnit_M2M_public.BusinessUnits_Busines");

                entity.HasOne(d => d.ServiceIdentityUser)
                    .WithMany(p => p.IdentityUserBusiUnitM2m)
                    .HasForeignKey(d => d.ServiceIdentityUserId)
                    .HasConstraintName("FK_public.IdentityUserBusiUnit_M2M_public.AspNetUsers_ServiceId");
            });

            modelBuilder.Entity<MigrationHistory>(entity =>
            {
                entity.HasKey(e => new { e.MigrationId, e.ContextKey })
                    .HasName("PK_public.__MigrationHistory");

                entity.ToTable("__MigrationHistory");

                entity.Property(e => e.MigrationId).HasMaxLength(150);

                entity.Property(e => e.ContextKey).HasMaxLength(300);

                entity.Property(e => e.Model).IsRequired();

                entity.Property(e => e.ProductVersion)
                    .IsRequired()
                    .HasMaxLength(32);
            });

            modelBuilder.Entity<RoleOperations>(entity =>
            {
                entity.HasIndex(e => e.RefSourceId)
                    .HasName("RoleOperations_IX_RefSourceId");

                entity.HasIndex(e => e.ServiceIdentityRoleId)
                    .HasName("ServiceUserOperations_IX_Role_Id");

                entity.Property(e => e.Id).HasDefaultValueSql("uuid_generate_v4()");

                entity.Property(e => e.ServiceIdentityRoleId)
                    .HasColumnName("ServiceIdentityRole_Id")
                    .HasMaxLength(128);

                entity.HasOne(d => d.RefSource)
                    .WithMany(p => p.RoleOperations)
                    .HasForeignKey(d => d.RefSourceId)
                    .HasConstraintName("FK_public.RoleOperations_public.Sources_RefSourceId");

                entity.HasOne(d => d.ServiceIdentityRole)
                    .WithMany(p => p.RoleOperations)
                    .HasForeignKey(d => d.ServiceIdentityRoleId)
                    .HasConstraintName("FK_public.ServiceUserOperations_public.AspNetRoles_Role_Id");
            });

            modelBuilder.Entity<SiteDevices>(entity =>
            {
                entity.HasIndex(e => e.AsSubjectOfBusinessUnitId)
                    .HasName("SiteDevices_IX_AsSubjectOfBusinessUnitId");

                entity.HasIndex(e => e.HardwareIdentity)
                    .HasName("SiteDevices_IX_HardwareIdentity")
                    .IsUnique();

                entity.HasIndex(e => e.ParentSiteDeviceId)
                    .HasName("SiteDevices_IX_ParentSiteDeviceId");

                entity.Property(e => e.Id).HasDefaultValueSql("uuid_generate_v4()");

                entity.Property(e => e.Brand).HasMaxLength(100);

                entity.Property(e => e.DeviceName).HasMaxLength(50);

                entity.Property(e => e.Model).HasMaxLength(100);

                entity.HasOne(d => d.AsSubjectOfBusinessUnit)
                    .WithMany(p => p.SiteDevices)
                    .HasForeignKey(d => d.AsSubjectOfBusinessUnitId)
                    .HasConstraintName("FK_public.SiteDevices_public.BusinessUnits_AsSubjectOfBusinessU");

                entity.HasOne(d => d.ParentSiteDevice)
                    .WithMany(p => p.InverseParentSiteDevice)
                    .HasForeignKey(d => d.ParentSiteDeviceId)
                    .HasConstraintName("FK_public.SiteDevices_public.SiteDevices_ParentSiteDeviceId");
            });

            modelBuilder.Entity<Sources>(entity =>
            {
                entity.HasIndex(e => e.SourceId)
                    .HasName("Sources_IX_SourceType")
                    .IsUnique();

                entity.Property(e => e.Id).HasDefaultValueSql("uuid_generate_v4()");
            });

            modelBuilder.Entity<UserSubIdentities>(entity =>
            {
                entity.HasIndex(e => e.ServiceIdentityUserId)
                    .HasName("UserSubIdentities_IX_ServiceIdentityUser_Id");

                entity.HasIndex(e => new { e.Type, e.Identity })
                    .HasName("UserSubIdentities_IX_UserSubIdTypeAndId");

                entity.Property(e => e.Id).HasDefaultValueSql("uuid_generate_v4()");

                entity.Property(e => e.Identity)
                    .IsRequired()
                    .HasMaxLength(100);

                entity.Property(e => e.ServiceIdentityUserId).HasMaxLength(128);

                entity.Property(e => e.Type)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.HasOne(d => d.ServiceIdentityUser)
                    .WithMany(p => p.UserSubIdentities)
                    .HasForeignKey(d => d.ServiceIdentityUserId)
                    .HasConstraintName("FK_public.UserSubIdentities_public.AspNetUsers_ServiceIdentityU");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
