using LSFE.Infrastructure.Dto.Planning;
using LSFE.Infrastructure.Entities;
using LSFE.Infrastructure.Entities.Admin;
using LSFE.Infrastructure.Entities.Auth;
using LSFE.Infrastructure.Entities.Doctor;
using LSFE.Infrastructure.Entities.Maintenance;
using LSFE.Infrastructure.Entities.Planning;
using LSFE.Infrastructure.Entities.Reports;
using LSFE.Infrastructure.Entities.TimeKeeping;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LSFE.Infrastructure.Database
{
    public class LSFEDbContext : IdentityDbContext<AppsUsers, Role, string>
    {
        public LSFEDbContext(DbContextOptions<LSFEDbContext> options) : base(options) { }
        public DbSet<Doctor> Doctors { get; set; }
        public DbSet<ForDoctorApproval> ForDoctorApprovals { get; set; }
        public DbSet<Departments> Departments { get; set; }
        public DbSet<Attachment> Attachments { get; set; }
        public DbSet<Areas> Areas { get; set; }
        public DbSet<Districts> Districts { get; set; }
        public DbSet<Institutions> Institutions { get; set; }
        public DbSet<Specialization> Specializations { get; set; }
        public DbSet<InstitutionCategory> InstitutionCategories { get; set; }
        public DbSet<Products> Products { get; set; }
        public DbSet<ProductTransaction> ProductTransactions { get; set; }
        public DbSet<InventoryTransaction> InventoryTransactions { get; set; }
        public DbSet<Territories> Territories { get; set; }
        public DbSet<Divisions> Divisions { get; set; }
        public DbSet<MRRawPlan> MRRawPlans { get; set; }
        public DbSet<MRRawPlansDto> MRRawPlansDto { get; set; }      
        public DbSet<CatchUpCall> CatchUpCalls { get; set; }
        public DbSet<Inventory> Inventory { get; set; }
        public DbSet<CatchUpCallReport> CatchUpCallReports { get; set; }
        public DbSet<CallRateReport> CallRateReports { get; set; }
        public DbSet<CallReachReport> CallReachReports { get; set; }
        public DbSet<ActualCoverageReport> ActualCoverageReports { get; set; }
        public virtual DbSet<DoctorWithPlan> DoctorWithPlan { get; set; }
        public DbSet<DoctorCoverageToday> DoctorCoverageTodays { get; set; }
        public virtual DbSet<PendingApproval> PendingApprovals { get; set; }
        public virtual DbSet<DoctorById> DoctorByIds { get; set; }
        public DbSet<MRPlanDetail> MRPlanDetails { get; set; }
        public DbSet<MRPlanHeader> MRPlanHeaders { get; set; }
        public DbSet<Attendance> Attendances { get; set; }
        public virtual DbSet<Entities.TimeKeeping.AttendanceDto> AttendanceDto { get; set; }
        public DbSet<ErrorLogs> ErrorLogs { get; set; }
        public DbSet<UserDevice> UserDevices { get; set; }
        public DbSet<AuthorizeRoles> AuthorizeRoles { get; internal set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<ActualCoverageReport>(entity =>
            {
                entity.ToTable("ActualCoverageReport");
                entity.HasNoKey();
            });
            modelBuilder.Entity<Areas>()
             .Property(d => d.AreaId)
             .ValueGeneratedOnAdd();

            modelBuilder.Entity<Divisions>()
             .Property(d => d.DivisionId)
             .ValueGeneratedOnAdd();

            modelBuilder.Entity<Doctor>()
            .HasOne(d => d.Institution)
            .WithMany(i => i.Doctors)
            .HasForeignKey(d => d.InstitutionId);

            modelBuilder.Entity<Doctor>()
           .HasOne(d => d.Districts)
           .WithMany(i => i.Doctors)
           .HasForeignKey(d => d.DistrictId);

            modelBuilder.Entity<Districts>(b =>
            {
                b.ToTable("Districts");
                b.HasOne(u => u.Areas)
                  .WithMany()
                 .HasForeignKey(u => u.AreaId)
                 .IsRequired(false);
            });

            modelBuilder.Entity<Institutions>(b =>
            {
                b.ToTable("Institutions");
                b.HasOne(u => u.Territories)
                  .WithMany()
                 .HasForeignKey(u => u.TerritoryId)
                 .IsRequired(false);
            });

            modelBuilder.Entity<Territories>(b =>
            {
                b.ToTable("Territories");
                b.HasOne(u => u.Districts)
                  .WithMany()
                 .HasForeignKey(u => u.DistrictId)
                 .IsRequired(false);
            });

            modelBuilder.Entity<AppsUsers>(b =>
            {
                b.ToTable("Users");
                b.HasOne(u => u.Attachment)
                  .WithMany()
                 .HasForeignKey(u => u.AttachmentId)
                 .IsRequired(false);

                b.HasOne(u => u.Department)
                 .WithMany()
                 .HasForeignKey(u => u.DepartmentId)
                 .IsRequired(false);
            });
            modelBuilder.Entity<Attachment>(b =>
            {
                b.ToTable("Attachment");

                b.Property(d => d.AttachmentId)
                 .ValueGeneratedOnAdd();
            });
            modelBuilder.Entity<Departments>(b =>
            {
                b.ToTable("Departments");
            });
            modelBuilder.Entity<IdentityUserClaim<string>>(b =>
            {
                b.ToTable("UserClaims");
            });
            modelBuilder.Entity<IdentityUserToken<string>>(b =>
            {
                b.ToTable("UserTokens");
            });
            modelBuilder.Entity<Role>(b =>
            {
                b.ToTable("Roles");
            });
            modelBuilder.Entity<IdentityUserRole<string>>(b =>
            {
                b.ToTable("UserRoles");
            });
        }
    }
}