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

namespace AyaNova.Models
{
    public partial class AyContext : DbContext
    {
        public virtual DbSet<SchemaVersion> SchemaVersion { get; set; }
        public virtual DbSet<MetricMM> MetricMM { get; set; }
        public virtual DbSet<MetricDD> MetricDD { get; set; }
        public virtual DbSet<User> User { get; set; }
        public virtual DbSet<UserOptions> UserOptions { get; set; }
        public virtual DbSet<Widget> Widget { get; set; }
        public virtual DbSet<GlobalBizSettings> GlobalBizSettings { get; set; }
        public virtual DbSet<GlobalOpsBackupSettings> GlobalOpsBackupSettings { get; set; }
        public virtual DbSet<GlobalOpsNotificationSettings> GlobalOpsNotificationSettings { get; set; }
        public virtual DbSet<Event> Event { get; set; }
        public virtual DbSet<SearchDictionary> SearchDictionary { get; set; }
        public virtual DbSet<SearchKey> SearchKey { get; set; }
        public virtual DbSet<FileAttachment> FileAttachment { get; set; }
        public virtual DbSet<OpsJob> OpsJob { get; set; }
        public virtual DbSet<OpsJobLog> OpsJobLog { get; set; }
        public virtual DbSet<Translation> Translation { get; set; }
        public virtual DbSet<TranslationItem> TranslationItem { get; set; }
        public virtual DbSet<DataListView> DataListView { get; set; }
        public virtual DbSet<Tag> Tag { get; set; }
        public virtual DbSet<FormCustom> FormCustom { get; set; }
        public virtual DbSet<PickListTemplate> PickListTemplate { get; set; }
        public virtual DbSet<License> License { get; set; }
        public virtual DbSet<Customer> Customer { get; set; }
        public virtual DbSet<Contract> Contract { get; set; }
        public virtual DbSet<HeadOffice> HeadOffice { get; set; }
        public virtual DbSet<LoanUnit> LoanUnit { get; set; }
        public virtual DbSet<NotifySubscription> NotifySubscription { get; set; }
        public virtual DbSet<NotifyEvent> NotifyEvent { get; set; }
        public virtual DbSet<Notification> Notification { get; set; }
        public virtual DbSet<NotifyDeliveryLog> NotifyDeliveryLog { get; set; }
        public virtual DbSet<Part> Part { get; set; }
        public virtual DbSet<PM> PM { get; set; }
        public virtual DbSet<PMItem> PMItem { get; set; }
        public virtual DbSet<PMTemplate> PMTemplate { get; set; }
        public virtual DbSet<PMTemplateItem> PMTemplateItem { get; set; }
        public virtual DbSet<Project> Project { get; set; }
        public virtual DbSet<PurchaseOrder> PurchaseOrder { get; set; }
        public virtual DbSet<Quote> Quote { get; set; }
        public virtual DbSet<QuoteItem> QuoteItem { get; set; }
        public virtual DbSet<QuoteTemplate> QuoteTemplate { get; set; }
        public virtual DbSet<QuoteTemplateItem> QuoteTemplateItem { get; set; }
        public virtual DbSet<Unit> Unit { get; set; }
        public virtual DbSet<UnitModel> UnitModel { get; set; }
        public virtual DbSet<Vendor> Vendor { get; set; }
        //WorkOrder
        public virtual DbSet<WorkOrder> WorkOrder { get; set; }
        public virtual DbSet<WorkOrderItem> WorkOrderItem { get; set; }
        public virtual DbSet<WorkOrderItemExpense> WorkOrderItemExpense { get; set; }
        public virtual DbSet<WorkOrderItemLabor> WorkOrderItemLabor { get; set; }
        public virtual DbSet<WorkOrderItemLoan> WorkOrderItemLoan { get; set; }
        public virtual DbSet<WorkOrderItemPart> WorkOrderItemPart { get; set; }
        public virtual DbSet<WorkOrderItemPartRequest> WorkOrderItemPartRequest { get; set; }
        public virtual DbSet<WorkOrderItemScheduledUser> WorkOrderItemScheduledUser { get; set; }
        public virtual DbSet<WorkOrderItemTask> WorkOrderItemTask { get; set; }
        public virtual DbSet<WorkOrderItemTravel> WorkOrderItemTravel { get; set; }
        public virtual DbSet<WorkOrderItemUnit> WorkOrderItemUnit { get; set; }


        //WorkOrderTemplate
        public virtual DbSet<WorkOrderTemplate> WorkOrderTemplate { get; set; }
        public virtual DbSet<WorkOrderTemplateItem> WorkOrderTemplateItem { get; set; }


        public virtual DbSet<Logo> Logo { get; set; }
        public virtual DbSet<Report> Report { 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 AyContext(DbContextOptions<AyContext> options) : base(options)
        { }



        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            //AUTOMATICALLY MATCH NAMES
            //https://andrewlock.net/customising-asp-net-core-identity-ef-core-naming-conventions-for-postgresql/
            foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {
                // Replace table names
                //entity.Relational().TableName = "a" + entity.Relational().TableName.ToLowerInvariant();
                entity.SetTableName("a" + entity.GetTableName().ToLowerInvariant());

                // Replace column names            
                foreach (var property in entity.GetProperties())
                {
                    //Any object that has a concurrencytoken field
                    //set it up to work properly with PostgreSQL
                    if (property.Name == "Concurrency")
                    {
                        property.SetColumnName("xmin");
                        property.SetColumnType("xid");
                        // property.Relational().ColumnName = "xmin";
                        // property.Relational().ColumnType = "xid";
                        property.ValueGenerated = ValueGenerated.OnAddOrUpdate;
                        property.IsConcurrencyToken = true;
                    }
                    else
                        property.SetColumnName(property.Name.ToLowerInvariant());
                }

                foreach (var key in entity.GetKeys())
                {
                    key.SetName(key.GetName().ToLowerInvariant());
                    // key.Relational().Name = key.Relational().Name.ToLowerInvariant();
                }

                foreach (var key in entity.GetForeignKeys())
                {
                    //key.Relational().Name = key.Relational().Name.ToLowerInvariant();

                    key.SetConstraintName(key.GetConstraintName().ToLowerInvariant());
                }

                foreach (var index in entity.GetIndexes())
                {
                    index.SetName(index.GetName().ToLowerInvariant());
                    //index.Relational().Name = index.Relational().Name.ToLowerInvariant();
                }
            }

            ///////////////////////////////
            //SERIALIZED OBJECTS
            //
            modelBuilder.Entity<Widget>().Property(z => z.Serial).UseIdentityByDefaultColumn();
            modelBuilder.Entity<WorkOrder>().Property(z => z.Serial).UseIdentityByDefaultColumn();
            modelBuilder.Entity<Quote>().Property(z => z.Serial).UseIdentityByDefaultColumn();
            modelBuilder.Entity<PM>().Property(z => z.Serial).UseIdentityByDefaultColumn();
            //modelBuilder.Entity<PurchaseOrder>().Property(z => z.Serial).UseIdentityByDefaultColumn();


            //modelBuilder.Entity<MetricCPU>().HasNoKey();

            ///////////////////////////////////////////////////////////////////////
            //TODO: this entire block is almost certainly wrong or not required

            // //Indexes must be specified through fluent api unfortunately
            // modelBuilder.Entity<FileAttachment>().HasIndex(z => z.StoredFileName);

            // //Relationships
            // modelBuilder.Entity<Translation>()
            //     .HasMany(z => z.TranslationItems)
            //     .WithOne(z => z.Translation)
            //     .IsRequired();//default delete behaviour is cascade when set to isrequired

            // modelBuilder.Entity<User>()
            //            .HasOne(z => z.UserOptions)
            //            .WithOne(z => z.User)
            //            .HasForeignKey<UserOptions>(z => z.UserId)
            //            .OnDelete(DeleteBehavior.Cascade);//Hopefully will delete the useroptions with the user?

            // //User->Widget Not certain about this definition
            // modelBuilder.Entity<User>()
            //           .HasOne(z => z.Widget)
            //           .WithOne(z => z.User)
            //           .HasForeignKey<Widget>(z => z.UserId)
            //           .OnDelete(DeleteBehavior.NoAction);
            //////////////////////////////////////////////////////////////






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

    }

}