using Gremlin.GremlinData.EntityClasses;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Gremlin.GremlinData.DBClasses
{
    public class AccountConfiguration : IEntityTypeConfiguration<Account>
    {

        public void Configure(EntityTypeBuilder<Account> entity)
        {
            entity.HasKey(e => e.AccountId);
            entity.HasMany(d => d.Contacts).WithOne(p => p.Account).IsRequired(true);
            entity.HasOne(d => d.AccountType).WithMany(p => p.Accounts).IsRequired(true);
            entity.HasOne(d => d.SubMarket).WithMany(p => p.Accounts).IsRequired(true);
            entity.HasAlternateKey(e => e.SAPAccountNumber);        // =Unique
            entity.Property(e => e.AccountId)
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.ParentAccountId);
            entity.Property(e => e.DataCreationDate)
                                      .HasColumnType("TIMESTAMP")
                                      .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                      .ValueGeneratedOnAdd()
                                      ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    ;
            entity.Property(e => e.AccountName)
                                    .IsRequired(true)
                                    //.HasColumnType("VARCHAR(250)")  
                                    .HasMaxLength(250) //impliziert (mit diesem Wert?) VARCHAR
                                    ;
            entity.Property(e => e.Notes);
            entity.Property(e => e.Street)
                                    .IsRequired(true)
                                    .HasMaxLength(100)  //impliziert (mit diesem Wert?) VARCHAR
                                    ;
            //entity.Property(e => e.Streetnumber)
            //                        .IsRequired(true)
            //                        .HasMaxLength(20)
            //                        ;
            entity.Property(e => e.ZIP)
                                    .IsRequired(true)
                                    .HasColumnType("Char(5)")
                                    //.HasMaxLength(5)  //impliziert (mit diesem Wert?) VARCHAR
                                    ;
            entity.Property(e => e.City)
                                    .IsRequired(true)
                                    .HasMaxLength(50)
                                    ;
            entity.Property(e => e.FloorOrBuilding)
                                    //.HasColumnType("VARCHAR(50)")  
                                    .HasMaxLength(50)  //impliziert (mit diesem Wert?) VARCHAR
                                    ;
            entity.Property(e => e.Longitude)
            //                        .HasDefaultValue(null)
                                    ;
            entity.Property(e => e.Latitude)
            //                        .HasDefaultValue(null)
                                    ;
            entity.Property(e => e.PhoneNumber)
                                    .IsRequired(true)
                                    .HasMaxLength(30)
                                    ;
            entity.Property(e => e.FaxNumber)
                                    .HasMaxLength(30)
                                    ;
            entity.Property(e => e.Webpage)
                                    //.HasColumnType("VARCHAR(250)")
                                    .HasMaxLength(250)  //impliziert (mit diesem Wert?) VARCHAR
                                    ;
            entity.Property(e => e.EMail)
                                        //.IsRequired(true)
                                        .HasMaxLength(150)  //impliziert (mit diesem Wert?) VARCHAR
                                        ;
            //entity.Property(e => e.DateOfCreationInSAP);
            //entity.Property(e => e.SubMarketCode);
            //entity.Property(e => e.SubMarket);
            //entity.Property(e => e.AccountType)
            //                       .IsRequired(true)
            //                       .HasColumnType("Char(3)")
            //                       ;
            //entity.Property(e => e.SubMarket)
            //                       .IsRequired(true)
            //                       .HasColumnType("Char(3)")
            //                       ;
            //entity.Property(e => e.LinkToSAP)
            //                        .HasColumnType("Text");
            // --> in DbHelper mit eigener Funktion aus den SAP-Ids konstruieren.
            entity.Property(e => e.SAPAccountNumber)
                                 .IsRequired(true)
                                 ;
            entity.Property(e => e.AccountCreatedInSAPOn)
                                   .IsRequired(true)
                                   ;


        }
    }

    public class ContactConfiguration : IEntityTypeConfiguration<Contact>
    {
        public void Configure(EntityTypeBuilder<Contact> entity)
        {
            entity.HasKey(e => e.ContactId);
            //entity.HasMany(d => d.CommunicationChannels).WithOne(p => p.Contact).IsRequired(true);
            entity.HasOne(p => p.Account).WithMany(d => d.Contacts).IsRequired(true);
            //entity.HasAlternateKey(e => e.SAPContactNumber);
            entity.Property(e => e.ContactId);
            entity.Property(e => e.DataCreationDate)
                                     .HasColumnType("TIMESTAMP")
                                     .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.SAPContactNumber)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.AcademicTitle);
            entity.Property(e => e.FirstName);
            entity.Property(e => e.LastName)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.Gender)
                                    //.IsRequired(true)         //darf nicht gesetzt werden, da sonst vom  DB-Engine NULL nicht erlaubt wird (trotz Definition als Bool? im Code. MySQL kennt kein Bool, sondern wandelt das intern in Tinyint um).
                                    ;
            entity.Property(e => e.OptInStatus)
                                    .HasDefaultValue(null)
                                    ;
            entity.Property(e => e.IsReference)
                                    .HasDefaultValue(false)
                                    ;
            entity.Property(e => e.Notes);
            entity.Property(e => e.LinkToSAP);
            entity.Property(e => e.ValidatedContact)
                                    .HasDefaultValue(false)
                                    ;
        }
    }

    //public class CommunicationChannelConfiguration : IEntityTypeConfiguration<CommunicationChannel>
    //{
    //    public void Configure(EntityTypeBuilder<CommunicationChannel> entity)
    //    {
    //        entity.HasKey("CommunicationChannelId");
    //        entity.HasOne(p => p.Contact).WithMany(e => e.CommunicationChannels).HasForeignKey("ContactId").IsRequired(true);

    //        entity.Property(e => e.DataCreationDate)
    //                                  .HasColumnType("TIMESTAMP")
    //                                  .HasDefaultValueSql("CURRENT_TIMESTAMP")
    //                                  .ValueGeneratedOnAdd()
    //                                  ;
    //        entity.Property(e => e.DataModificationDate)
    //                                .HasColumnType("TIMESTAMP")
    //                                .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
    //                                .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
    //                                .IsConcurrencyToken(true)
    //                                ;
    //        entity.Property(e => e.DataValidFrom)
    //                                .HasColumnType("DATETIME")
    //                                .HasDefaultValueSql("CURRENT_TIMESTAMP")
    //                                .ValueGeneratedOnAdd()
    //                                ;
    //        entity.Property(e => e.DataModificationByUser)
    //                                .HasColumnType("TINYTEXT")
    //                                .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
    //                                ;
    //        entity.Property(e => e.DataValidUntil)
    //                                .HasColumnType("DATETIME")
    //                                .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
    //                                .ValueGeneratedOnAdd()
    //                                ;
    //        entity.Property(e => e.DataVersionNumber)
    //                                .HasDefaultValue(1)
    //                                .IsRequired(true)
    //                                ;
    //        entity.Property(e => e.DataVersionComment)
    //                                .HasDefaultValue("")
    //                                ;
    //        entity.Property(e => e.DataStatus)
    //                                //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
    //                                .IsRequired(true)
    //                                ;

    //        entity.Property(e => e.Typ)
    //                                .IsRequired()
    //                                .HasMaxLength(50)
    //                                ;
    //        entity.Property(e => e.Number)
    //                                .IsRequired()
    //                                .HasMaxLength(250)
    //                                ;
    //        entity.Property(e => e.IsPreferred)
    //                                .HasDefaultValue(false)
    //                                ;
    //    }
    //}

    public class QuoteConfiguration : IEntityTypeConfiguration<Quote>
    {
        public void Configure(EntityTypeBuilder<Quote> entity)
        {
            entity.HasKey(e => e.QuoteId);
            entity.HasMany(d => d.LineItems).WithOne(p => p.Quote);
            //entity.HasOne(p => p.Recipient).WithMany(e => e.Quotes);
            entity.Property(e => e.DataCreated)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModified)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.QuotationNumber)
                                     .HasColumnType("VARCHAR(255)")
                                     .IsRequired(true)
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.QuotationDate)
                                    .IsRequired(true)
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.ValidUntil);
            entity.Property(e => e.ValidFor)
                                    //.HasColumnType("TINYINT")
                                    .IsRequired(true)
                                    ;
            //entity.Property(e => e.SalesRep);
            entity.Ignore("SalesRep");

            entity.Property(e => e.TotalListprice);
            entity.Property(e => e.TotalDiscount);
            entity.Property(e => e.TotalNet);
            entity.Property(e => e.VAT)
                                    //.HasDefaultValue("19")
                                    ;
            entity.Property(e => e.TotalGross);
            entity.Property(e => e.QuoteContains3PP)
                                    .HasDefaultValue(false);
            entity.Property(e => e.QuoteContainsRB)
                                    .HasDefaultValue(false);
            entity.Property(e => e.QuoteTemplate);
        }
    }

    public class LineItemConfiguration : IEntityTypeConfiguration<LineItem>
    {
        public void Configure(EntityTypeBuilder<LineItem> entity)
        {
            entity.HasKey(e => e.QuoteId);
            entity.HasOne(p => p.Quote).WithMany(d => d.LineItems).HasForeignKey(fk => fk.QuoteId);
            entity.Property(e => e.DataCreated)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModified)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.Position)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.Amount)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.ProductNumber)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.OptionNumber);
            entity.Property(e => e.SapShortDescription);
            entity.Property(e => e.SapLongDescription);
            entity.Property(e => e.ProductLine);
            entity.Property(e => e.TotalDiscount)
                                    .IsRequired(true)
                                    .HasDefaultValue(0);
            entity.Property(e => e.SalesDiscount)
                                    .IsRequired(true)
                                    .HasDefaultValue(0);
            entity.Property(e => e.PromotionalDiscount)
                                    .IsRequired(true)
                                    .HasDefaultValue(0);
            entity.Property(e => e.ContractualDiscount)
                                    .IsRequired(true)
                                    .HasDefaultValue(0);
            entity.Property(e => e.DemoDiscount)
                                    .IsRequired(true)
                                    .HasDefaultValue(0);
            entity.Property(e => e.ListPrice)
                                    .IsRequired(true);
            entity.Property(e => e.ExtendedListPrice)
                                    .IsRequired(true);
            entity.Property(e => e.NetPrice)
                                    .IsRequired(true);
            entity.Property(e => e.Total)
                                    .IsRequired(true);
        }
    }

    public class ProductConfiguration : IEntityTypeConfiguration<Product>
    {
        public void Configure(EntityTypeBuilder<Product> entity)
        {
            entity.HasKey(e => e.ProductId);
            entity.HasOne(d => d.CustomDescription).WithOne(p => p.Product);
            //entity.HasOne(p => p.ProductLine).WithMany(d => d.Products);
            entity.Property(e => e.DataCreationDate)
                                     .HasColumnType("TIMESTAMP")
                                     .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    .ValueGeneratedOnAddOrUpdate()
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.ProductNumber)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.OptionNumber);
            entity.Property(e => e.SapShortDescription);
            entity.Property(e => e.SapLongDescription);
            entity.Property(e => e.Weight);
            entity.Property(e => e.ProductStatus);
            entity.Property(e => e.IntroductionDate);
            entity.Property(e => e.ListPrice)
                                    .IsRequired(true);
        }
    }

    public class CustomDescriptionConfiguration : IEntityTypeConfiguration<CustomDescription>
    {
        public void Configure(EntityTypeBuilder<CustomDescription> entity)
        {
            entity.HasKey(e => e.CustomDescriptionId);
            entity.HasOne(p => p.Product).WithOne(d => d.CustomDescription)
                                    //.HasForeignKey("ProductId")
                                    ;
            entity.Property(e => e.DataCreationDate)
                                     .HasColumnType("TIMESTAMP")
                                     .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.ProductNumber)
                                    .IsRequired(true);
            entity.Property(e => e.OptionNumber);
            entity.Property(e => e.Heading)
                                    .IsRequired(true);
            entity.Property(e => e.DescriptionText);
            entity.Property(e => e.CoverletterText);
            entity.Property(e => e.Notes);
        }
    }

    public class ProductLineConfiguration : IEntityTypeConfiguration<ProductLine>
    {
        public void Configure(EntityTypeBuilder<ProductLine> entity)
        {
            //entity.HasKey(e => e.ProductLineId);  //Property removed. PK is AccountTypeCode (defined via data annotation in class).
            entity.HasMany(p => p.Products).WithOne(d => d.ProductLine);
            entity.Property(e => e.DataCreationDate)
                                     .HasColumnType("TIMESTAMP")
                                     .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
        }
    }

    public class AccountTypeConfiguration : IEntityTypeConfiguration<AccountType>
    {
        public void Configure(EntityTypeBuilder<AccountType> entity)
        {
            //entity.HasKey(e => e.AccountTypeCode);
            //entity.HasMany(p => p.Accounts).WithOne(d => d.AccountType); //already defined in class Account
            entity.Property(e => e.DataCreationDate)
                                     .HasColumnType("TIMESTAMP")
                                     .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.AccountTypeCode)
                                    .IsRequired(true)
                                    .HasColumnType("Char(3)")
                                    ;
            entity.Property(e => e.AccountTypeDescription)
                                    .HasColumnType("Varchar(1000)")
                                    //.IsRequired(true)
                                    ;
        }
    }

    public class SubMarketConfiguration : IEntityTypeConfiguration<SubMarket>
    {
        public void Configure(EntityTypeBuilder<SubMarket> entity)
        {
            //entity.HasKey(e => e.SubMarketCode);
            //entity.HasMany(p => p.Accounts).WithOne(d => d.SubMarket);  //already defined in class Account
            entity.Property(e => e.DataCreationDate)
                                     .HasColumnType("TIMESTAMP")
                                     .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                     .ValueGeneratedOnAdd()
                                     ;
            entity.Property(e => e.DataModificationDate)
                                    .HasColumnType("TIMESTAMP")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAddOrUpdate()      //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true)
                                    .IsConcurrencyToken(true)
                                    ;
            entity.Property(e => e.DataValidFrom)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("CURRENT_TIMESTAMP")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataModificationByUser)
                                    .HasColumnType("TINYTEXT")
                                    .HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()")
                                    ;
            entity.Property(e => e.DataValidUntil)
                                    .HasColumnType("DATETIME")
                                    .HasDefaultValueSql("'9999-12-31 23:59:59.000000'")
                                    .ValueGeneratedOnAdd()
                                    ;
            entity.Property(e => e.DataVersionNumber)
                                    .HasDefaultValue(1)
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.DataVersionComment)
                                    .HasDefaultValue("")
                                    ;
            entity.Property(e => e.DataStatus)
                                    //.HasDefaultValue("Active")  //Default-Wert wird nicht gesetzt?!? Bug in EF Core?
                                    .IsRequired(true)
                                    ;
            entity.Property(e => e.SubMarketCode)
                                    //.IsRequired(true)         //implizit gesetzt durch PK.
                                    .HasColumnType("Char(3)")
                                    ;
            entity.Property(e => e.SubMarketDescription)
                                    .HasColumnType("Varchar(1000)")
                                    //.IsRequired(true)
                                    ;
        }
    }

    public class RegisteredUserConfiguration : IEntityTypeConfiguration<RegisteredUser>
    {
        public void Configure(EntityTypeBuilder<RegisteredUser> builder)
        {
            builder.HasKey(e => e.RegisteredUserID);

            builder.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd();
            builder.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(true);
            builder.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()");

            builder.HasMany(d => d.RUSettings).WithOne(p => p.RegisteredUser).IsRequired(true);
            builder.Property(e => e.UserName).IsRequired(true);
            builder.Property(e => e.PasswordHash).IsRequired(true);
        }
    }

    public class RUSettingsConfiguration : IEntityTypeConfiguration<RUSetting>
    {
        public void Configure(EntityTypeBuilder<RUSetting> builder)
        {
            builder.HasKey(e => e.RUSettingsID);

            builder.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd();
            builder.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(true);
            builder.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()");

            builder.HasOne(d => d.RegisteredUser).WithMany(p => p.RUSettings).IsRequired(true);
            builder.Property(e => e.SettingKey).IsRequired(true);
            builder.Property(e => e.SettingValue).IsRequired(true);
        }
    }
}
