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; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<FccTankInfo>()
                .HasOne(tankInfos => tankInfos.Oil)
                .WithMany(oilInfo => oilInfo.FccTankInfo)
                .HasForeignKey(tankInfo => tankInfo.OilId)
                .OnDelete(DeleteBehavior.Restrict);

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

            base.OnModelCreating(modelBuilder);
        }
    }
}
