using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using rockfishCore.Util;
using Microsoft.Extensions.Logging;

namespace rockfishCore.Models
{
    public partial class rockfishContext : DbContext
    {

      
        public virtual DbSet<Customer> Customer { get; set; }
        public virtual DbSet<LicenseTemplates> LicenseTemplates { get; set; }
        public virtual DbSet<Purchase> Purchase { get; set; }
        public virtual DbSet<Site> Site { get; set; }
        public virtual DbSet<User> User { get; set; }

        //schema 2
        public virtual DbSet<Product> Product { get; set; }

        //schema 4
        public virtual DbSet<TextTemplate> TextTemplate { get; set; }

        //schema 6
        public virtual DbSet<RfCase> RfCase { get; set; }
        public virtual DbSet<RfCaseBlob> RfCaseBlob { get; set; }
        public virtual DbSet<RfCaseProject> RfCaseProject { get; set; }

        //schema 10 case 3233
        public virtual DbSet<License> License { get; set; }



        //Note: had to add this constructor to work with the code in startup.cs that gets the connection string from the appsettings.json file
        //and commented out the above on configuring
        public rockfishContext(DbContextOptions<rockfishContext> options) : base(options)
        { }

        //************************************************
        //add logging to diagnose sql errors
        //TODO:comment out  this entire method in production
        // protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        // {
        //     var lf = new LoggerFactory();
        //     lf.AddProvider(new rfLoggerProvider());
        //     optionsBuilder.UseLoggerFactory(lf);
        //     optionsBuilder.EnableSensitiveDataLogging();//show parameters in log text
        // }
        //************************************************

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {           
            modelBuilder.Entity<Customer>(entity =>
            {
                entity.ToTable("customer");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.AffiliateNumber)
                    .HasColumnName("affiliateNumber")
                    .HasColumnType("text");

                entity.Property(e => e.Country)
                    .HasColumnName("country")
                    .HasColumnType("text");

                entity.Property(e => e.DoNotContact)
                    .IsRequired()
                    .HasColumnName("doNotContact")
                    .HasColumnType("boolean");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.Notes)
                    .HasColumnName("notes")
                    .HasColumnType("text");

                entity.Property(e => e.StateProvince)
                    .HasColumnName("stateProvince")
                    .HasColumnType("text");

                entity.Property(e => e.Active)
                    .IsRequired()
                    .HasColumnName("active")
                    .HasColumnType("boolean");

                entity.Property(e => e.AdminEmail)
                  .HasColumnName("adminEmail")
                  .HasColumnType("text");

                entity.Property(e => e.SupportEmail)
              .HasColumnName("supportEmail")
              .HasColumnType("text");

              
            });

            
            modelBuilder.Entity<LicenseTemplates>(entity =>
            {
                entity.ToTable("licenseTemplates");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.FullAddOn)
                    .HasColumnName("fullAddOn")
                    .HasColumnType("text");

                entity.Property(e => e.FullNew)
                    .HasColumnName("fullNew")
                    .HasColumnType("text");

                entity.Property(e => e.FullTrial)
                    .HasColumnName("fullTrial")
                    .HasColumnType("text");

                entity.Property(e => e.FullTrialGreeting)
                    .HasColumnName("fullTrialGreeting")
                    .HasColumnType("text");

                entity.Property(e => e.LiteAddOn)
                    .HasColumnName("liteAddOn")
                    .HasColumnType("text");

                entity.Property(e => e.LiteNew)
                    .HasColumnName("liteNew")
                    .HasColumnType("text");

                entity.Property(e => e.LiteTrial)
                    .HasColumnName("liteTrial")
                    .HasColumnType("text");

                entity.Property(e => e.LiteTrialGreeting)
                    .HasColumnName("liteTrialGreeting")
                    .HasColumnType("text");


            });

           

            modelBuilder.Entity<Purchase>(entity =>
            {
                entity.ToTable("purchase");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.CancelDate)
                    .HasColumnName("cancelDate")
                    .HasColumnType("integer");

                entity.Property(e => e.CouponCode)
                    .HasColumnName("couponCode")
                    .HasColumnType("text");

                entity.Property(e => e.CustomerId)
                    .HasColumnName("customer_id")
                    .HasColumnType("integer")
                     .IsRequired();

                entity.Property(e => e.Email)
                    .HasColumnName("email")
                    .HasColumnType("text");

                entity.Property(e => e.ExpireDate)
                    .HasColumnName("expireDate")
                    .HasColumnType("integer");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.Notes)
                    .HasColumnName("notes")
                    .HasColumnType("text");

                entity.Property(e => e.ProductCode)
                    .HasColumnName("productCode")
                    .HasColumnType("text");

                entity.Property(e => e.PurchaseDate)
                    .HasColumnName("purchaseDate")
                    .HasColumnType("integer");

                entity.Property(e => e.SalesOrderNumber)
                    .HasColumnName("salesOrderNumber")
                    .HasColumnType("text");

                entity.Property(e => e.SiteId)
                    .HasColumnName("site_id")
                    .HasColumnType("integer")
                     .IsRequired();

                entity.Property(e => e.VendorName)
                    .HasColumnName("vendorName")
                    .HasColumnType("text");

                entity.Property(e => e.RenewNoticeSent)
                    .IsRequired()
                    .HasColumnName("renewNoticeSent")
                    .HasColumnType("boolean");

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.Purchase)
                    .HasForeignKey(d => d.CustomerId)
                    .OnDelete(DeleteBehavior.Cascade);

                entity.HasOne(d => d.Site)
                    .WithMany(p => p.Purchase)
                    .HasForeignKey(d => d.SiteId)
                    .OnDelete(DeleteBehavior.Cascade);
            });

            modelBuilder.Entity<Site>(entity =>
            {
                entity.ToTable("site");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Country)
                    .HasColumnName("country")
                    .HasColumnType("text");

                entity.Property(e => e.CustomerId)
                    .HasColumnName("customer_id")
                    .HasColumnType("integer")
                    .IsRequired();

                entity.Property(e => e.DbType)
                    .HasColumnName("dbType")
                    .HasColumnType("text");

                entity.Property(e => e.HostName)
                    .HasColumnName("hostName")
                    .HasColumnType("text");

                entity.Property(e => e.Hosted)
                    .IsRequired()
                    .HasColumnName("hosted")
                    .HasColumnType("boolean");

                entity.Property(e => e.HostingEndDate)
                    .HasColumnName("hostingEndDate")
                    .HasColumnType("integer");

                entity.Property(e => e.HostingStartDate)
                    .HasColumnName("hostingStartDate")
                    .HasColumnType("integer");


                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.Networked)
                    .IsRequired()
                    .HasColumnName("networked")
                    .HasColumnType("boolean");

                entity.Property(e => e.Notes)
                    .HasColumnName("notes")
                    .HasColumnType("text");

                entity.Property(e => e.ServerBits)
                    .HasColumnName("serverBits")
                    .HasColumnType("text");

                entity.Property(e => e.ServerOs)
                    .HasColumnName("serverOS")
                    .HasColumnType("text");

                entity.Property(e => e.StateProvince)
                    .HasColumnName("stateProvince")
                    .HasColumnType("text");

                entity.Property(e => e.Trial)
                    .IsRequired()
                    .HasColumnName("trial")
                    .HasColumnType("boolean");

                entity.Property(e => e.TrialNotes)
                    .HasColumnName("trialNotes")
                    .HasColumnType("text");

                entity.Property(e => e.TrialStartDate)
                    .HasColumnName("trialStartDate")
                    .HasColumnType("integer");

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.Site)
                    .HasForeignKey(d => d.CustomerId)
                    .OnDelete(DeleteBehavior.Cascade);

                //.WillCascadeOnDelete(true);
            });

          


            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("user");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Login)
                    .IsRequired()
                    .HasColumnName("login")
                    .HasColumnType("text");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.DlKey)
                    .HasColumnName("dlkey")
                    .HasColumnType("text");

                entity.Property(e => e.DlKeyExp)
                    .HasColumnName("dlkeyexp")
                    .HasColumnType("integer");
            });



            modelBuilder.Entity<Product>(entity =>
            {
                entity.ToTable("Product");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.ProductCode)
                    .HasColumnName("productCode")
                    .HasColumnType("text");

                entity.Property(e => e.Price)
                        .HasColumnName("price")
                        .HasColumnType("integer");

                entity.Property(e => e.RenewPrice)
                    .HasColumnName("renewPrice")
                    .HasColumnType("integer");

            });



            modelBuilder.Entity<TextTemplate>(entity =>
            {
                entity.ToTable("TextTemplate");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.Template)
                        .HasColumnName("template")
                        .HasColumnType("text");

            });


            //Schema 6 case 3308
            modelBuilder.Entity<RfCaseProject>(entity =>
            {
                entity.ToTable("rfcaseproject");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                // entity.HasOne(cp => cp.RfCase)
                //     .WithMany();

            });



            modelBuilder.Entity<RfCaseBlob>(entity =>
            {
                entity.ToTable("rfcaseblob");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.RfCaseId)
                    .HasColumnName("rfcaseid")
                    .HasColumnType("integer")
                    .IsRequired();

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("text");

                entity.Property(e => e.File)
                    .HasColumnName("file")
                    .HasColumnType("blob")
                    .IsRequired();

              
            });



            modelBuilder.Entity<RfCase>(entity =>
            {
                entity.ToTable("rfcase");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Title)
                    .HasColumnName("title")
                    .HasColumnType("text")
                    .IsRequired();

                entity.Property(e => e.RfCaseProjectId)
                    .HasColumnName("rfcaseprojectid")
                    .HasColumnType("integer")
                    .IsRequired();

                entity.Property(e => e.Priority)
                    .IsRequired()
                    .HasColumnName("priority")
                    .HasColumnType("integer");

                entity.Property(e => e.Notes)
                    .HasColumnName("notes")
                    .HasColumnType("text");

                entity.Property(e => e.DtCreated)
                    .HasColumnName("dtcreated")
                    .HasColumnType("integer");

                entity.Property(e => e.DtClosed)
                    .HasColumnName("dtclosed")
                    .HasColumnType("integer");

                entity.Property(e => e.ReleaseVersion)
                    .HasColumnName("releaseversion")
                    .HasColumnType("text");

                entity.Property(e => e.ReleaseNotes)
                    .HasColumnName("releasenotes")
                    .HasColumnType("text");



            });





            //case 3233
            modelBuilder.Entity<License>(entity =>
           {
               entity.ToTable("license");

               entity.Property(e => e.Id).HasColumnName("id");

               entity.Property(e => e.DtCreated)
                  .HasColumnName("dtcreated")
                  .HasColumnType("integer")
                  .IsRequired();

               entity.Property(e => e.Code)
                   .HasColumnName("code")
                   .HasColumnType("text")
                   .IsRequired();

               entity.Property(e => e.CustomerId)
                   .HasColumnName("customerid")
                   .HasColumnType("integer")
                   .IsRequired();

               entity.Property(e => e.DtFetched)
                   .HasColumnName("dtfetched")
                   .HasColumnType("integer");

               entity.Property(e => e.Email)
                   .HasColumnName("email")
                   .HasColumnType("text")
                   .IsRequired();

               entity.Property(e => e.RegTo)
                .HasColumnName("regto")
                .HasColumnType("text")
                .IsRequired();

               entity.Property(e => e.Fetched)
                   .HasColumnName("fetched")
                   .HasColumnType("boolean")
                   .IsRequired();

               entity.Property(e => e.FetchFrom)
                   .HasColumnName("fetchfrom")
                   .HasColumnType("text");

               entity.Property(e => e.Key)
                   .HasColumnName("key")
                   .HasColumnType("text")
                   .IsRequired();




           });



            //-----------
        }
    }
}