using Microsoft.EntityFrameworkCore;
using Model.Models.VitrosModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LISWebAPI.Persistence
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<Result> ResultsData { get; set; }
        public DbSet<Sample> SampleData { get; set; }
        public DbSet<Component> ComponentMasterData { get; set; }
        public DbSet<Machine> Machines { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configuring with Fluent API
            modelBuilder.Entity<Result>().ToTable("LAB_LIS_SampleResult");
            modelBuilder.Entity<Sample>().ToTable("LAB_LIS_SampleData");
            modelBuilder.Entity<Component>().ToTable("LAB_LIS_ComponentMaster");
            modelBuilder.Entity<Machine>().ToTable("LIS_CFG_LabMachine");

            modelBuilder.Entity<Result>().HasKey(x => x.Id);

            modelBuilder.Entity<Sample>().HasMany(x => x.Results)
                                        .WithOne(x => x.Sample)
                                         .HasForeignKey(x => x.LisSampleId);

            modelBuilder.Entity<Result>().HasOne(x => x.Sample);



            modelBuilder.Entity<Component>().HasOne(x => x.Machine);


            modelBuilder.Entity<Machine>().HasMany(x => x.Components);
            modelBuilder.Entity<Result>().HasOne(x => x.Sample);

            modelBuilder.Entity<Sample>().HasOne(s => s.Machine)
                                         .WithMany(m => m.Samples)
                                         .HasForeignKey(s => s.MachineId);

            modelBuilder.Entity<Result>().HasOne(x => x.Machine);


            base.OnModelCreating(modelBuilder);
        }
    }
}
