using EnotaryoPH.Data.Entities;
using EnotaryoPH.Data.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace EnotaryoPH.Data
{
    public class NotaryoDBContext : DbContext
    {
        private readonly IConfiguration _configuration;

        public NotaryoDBContext(IConfiguration configuration, DbContextOptions<NotaryoDBContext> Options) : base(Options) => _configuration = configuration;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(_configuration.GetConnectionString("NotaryoDatabase"));

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
            base.OnModelCreating(modelBuilder);
        }

        public DbSet<ErrorLog> ErrorLogs { get; set; }
        public DbSet<EventLog> EventLogs { get; set; }
        public DbSet<IdentificationDocument> IdentificationDocuments { get; set; }
        public DbSet<Lawyer> Lawyers { get; set; }
        public DbSet<LawyerVideoConferenceParticipant> LawyerVideoConferenceParticipants { get; set; }
        public DbSet<LawyerVideoConferenceSchedule> LawyerVideoConferenceSchedules { get; set; }
        public DbSet<LookupData> LookupData { get; set; }
        public DbSet<LookupDataValue> LookupDataValues { get; set; }
        public DbSet<Template> Templates { get; set; }
        public DbSet<TransactionDeclineReason> TransactionDeclineReasons { get; set; }
        public DbSet<TransactionDocument> TransactionDocuments { get; set; }
        public DbSet<TransactionNotary> TransactionNotaries { get; set; }
        public DbSet<TransactionOTP> TransactionOTPs { get; set; }
        public DbSet<Transaction> Transactions { get; set; }
        public DbSet<TransactionSelfie> TransactionSelfies { get; set; }
        public DbSet<TransactionSignatory> TransactionSignatories { get; set; }
        public DbSet<TransactionSignatoryDeclineReason> TransactionSignatoryDeclineReasons { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<VideoRecording> VideoRecordings { get; set; }

        public TEntity UpdateOrCreate<TEntity>(TEntity entity) where TEntity : class
        {
            if (Entry(entity).State == EntityState.Detached)
            {
                Set<TEntity>().Add(entity);
            }
            else
            {
                Set<TEntity>().Update(entity);
            }
            return entity;
        }
    }
}