using DbFirst.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace DbFirst.Infrastructure;

public partial class ApplicationDbContext : DbContext
{
    private readonly TableConfigurations _config;

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IOptionsMonitor<TableConfigurations> configOptions)
        : base(options)
    {
        _config = configOptions.CurrentValue;
    }

    public virtual DbSet<VwmyCatalog> VwmyCatalogs { get; set; }
    public virtual DbSet<SmfLayout> SmfLayouts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var catCfg = _config.VwmyCatalog;

        modelBuilder.Entity<VwmyCatalog>(entity =>
        {
            entity.HasKey(e => e.Guid);

            entity.ToView(catCfg.ViewName);

            entity.Property(e => e.Guid).HasColumnName(catCfg.GuidColumnName);
            entity.Property(e => e.AddedWho)
                .HasMaxLength(30)
                .IsUnicode(false)
                .HasColumnName(catCfg.AddedWhoColumnName);
            entity.Property(e => e.AddedWhen)
                .HasColumnType("datetime")
                .HasColumnName(catCfg.AddedWhenColumnName);
            entity.Property(e => e.CatString)
                .HasMaxLength(900)
                .IsUnicode(false)
                .HasColumnName(catCfg.CatStringColumnName);
            entity.Property(e => e.CatTitle)
                .HasMaxLength(100)
                .IsUnicode(false)
                .HasColumnName(catCfg.CatTitleColumnName);
            entity.Property(e => e.ChangedWhen)
                .HasColumnType("datetime")
                .HasColumnName(catCfg.ChangedWhenColumnName);
            entity.Property(e => e.ChangedWho)
                .HasMaxLength(30)
                .IsUnicode(false)
                .HasColumnName(catCfg.ChangedWhoColumnName);
        });

        modelBuilder.Entity<SmfLayout>(entity =>
        {
            entity.HasKey(e => e.Guid);
            entity.ToTable("TBDD_SMF_LAYOUT", tb => tb.HasTrigger("TBDD_SMF_LAYOUT_AFT_UPD"));

            entity.Property(e => e.Guid).HasColumnName("GUID");
            entity.Property(e => e.Active).HasColumnName("ACTIVE");
            entity.Property(e => e.LayoutType)
                .HasMaxLength(50)
                .HasColumnName("LAYOUT_TYPE");
            entity.Property(e => e.LayoutKey)
                .HasMaxLength(150)
                .HasColumnName("LAYOUT_KEY");
            entity.Property(e => e.UserName)
                .HasMaxLength(50)
                .HasColumnName("USER_NAME");
            entity.Property(e => e.LayoutData).HasColumnName("LAYOUT_DATA");
            entity.Property(e => e.AddedWho)
                .HasMaxLength(50)
                .HasColumnName("ADDED_WHO");
            entity.Property(e => e.AddedWhen)
                .HasColumnType("datetime")
                .HasColumnName("ADDED_WHEN");
            entity.Property(e => e.ChangedWho)
                .HasMaxLength(50)
                .HasColumnName("CHANGED_WHO");
            entity.Property(e => e.ChangedWhen)
                .HasColumnType("datetime")
                .HasColumnName("CHANGED_WHEN");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
