using Edge.Core.Domain.FccMachineInfo;
using Edge.Core.Domain.FccNozzleInfo;
using Edge.Core.Domain.FccOilInfo;
using Edge.Core.Domain.FccOrderInfo;
using Edge.Core.Domain.FccStationInfo;
using Edge.Core.Domain.FccTankInfo;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Edge.Core.Core.database
{
    public class MysqlDbContext : DbContext
    {
        public MysqlDbContext() { }

        [ActivatorUtilitiesConstructor]
        public MysqlDbContext(DbContextOptions<MysqlDbContext> options) : base(options) { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                /* Default constructor with no parameter init this class will use below appsettings.json*/
                IConfigurationRoot configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.Development.json")
                   .Build();
                var connectionString = configuration.GetConnectionString("DefaultConnection");
                optionsBuilder.UseMySql(connectionString,ServerVersion.AutoDetect(connectionString));
            }
        }
        public DbSet<FccStationInfo> FccStationInfos { get; set; }

        public DbSet<FccOilInfo> OilInfos { get; set; }

        public DbSet<FccTankInfo> TankInfos { get; set; }

        public DbSet<FccNozzleInfo> NozzleInfos { get; set; }

        public DbSet<FccOrderInfo> FccOrderInfos { get; set; }

        public DbSet<FccMachineInfo> FccMachineInfos { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            

            modelBuilder.Entity<FccTankInfo>()
                .HasOne(tankInfos => tankInfos.Oil)
                .WithMany(oilInfo => oilInfo.FccTankInfo)
                .HasForeignKey(tankInfo => tankInfo.OilId)
                .IsRequired(false) // 显式配置外键为可空
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<FccNozzleInfo>()
                .HasOne(nozzleInfo => nozzleInfo.Tank)
                .WithMany(tankInfo => tankInfo.FccNozzleInfos)
                .HasForeignKey(nozzleInfo => nozzleInfo.TankId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<FccNozzleInfo>()
                .HasOne(nozzleInfo => nozzleInfo.FccMachineInfo)
                .WithMany(machine => machine.fccNozzleInfos)
                .HasForeignKey(nozzle => nozzle.MachineId)
                .OnDelete(DeleteBehavior.Restrict);

            //modelBuilder.Entity<FccOrderInfo>(entity =>
            //{
                
            //    entity.Property(order => order.EndTime).IsRequired(false);
            //    entity.Property(order => order.PaymentTime).IsRequired(false);
            //    entity.Property(order => order.PayType).IsRequired(false);
            //    entity.Property(order => order.CloundOrderId).IsRequired(false);
            //    entity.Property(order => order.AmountPayable).IsRequired(false);
            //    entity.Property(order => order.RefundAmount).IsRequired(false);
            //    entity.Property(order => order.UserName).IsRequired(false);
            //    entity.Property(order => order.PhoneNumber).IsRequired(false);
            //    entity.Property(order => order.PaymentName).IsRequired(false);
            //    entity.Property(order => order.PumpCode).IsRequired(false);
            //});

            base.OnModelCreating(modelBuilder);
        }
    }
}
