using Microsoft.EntityFrameworkCore;
using MTWorkHR.Core.Entities;
using MTWorkHR.Core.Entities.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTWorkHR.Infrastructure.Data
{
    public class HRDataContext : DbContext
    {
        public HRDataContext(DbContextOptions<HRDataContext> options) : base(options) { }

       // public DbSet<Company> Companies { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfigurationsFromAssembly(typeof(HRDataContext).Assembly);
            base.OnModelCreating(modelBuilder);
        }
        public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            foreach (var entry in base.ChangeTracker.Entries<AuditEntity>().Where(q=> q.State == EntityState.Added || q.State == EntityState.Modified))
            {
                entry.Entity.UpdateDate = DateTime.Now;
                if(entry.State == EntityState.Added)
                {
                    entry.Entity.CreateDate = DateTime.Now;
                }
            }
            return base.SaveChangesAsync(cancellationToken);
        }
       
    }
}