using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace API_Access_Control.Models.Entities;

public partial class CloudJwtContext : DbContext
{
    public CloudJwtContext()
    {
    }

    public CloudJwtContext(DbContextOptions<CloudJwtContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Api> Apis { get; set; }

    public virtual DbSet<Consommateur> Consommateurs { get; set; }

    public virtual DbSet<Jeton> Jetons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            var builder = new ConfigurationBuilder();
            builder.AddUserSecrets<Program>();

            var configuration = builder.Build();
            optionsBuilder.UseSqlServer(configuration["CLOUD_JWT_Connection"]);
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Api>(entity =>
        {
            entity.HasKey(e => e.ApiPk);

            entity.ToTable("Api");

            entity.Property(e => e.ApiPk).HasColumnName("Api_PK");
            entity.Property(e => e.ClefSecrete)
                .HasMaxLength(64)
                .IsUnicode(false)
                .HasColumnName("Clef_Secrete");
        });

        modelBuilder.Entity<Consommateur>(entity =>
        {
            entity.HasKey(e => e.ConsommateurPk);

            entity.ToTable("Consommateur");

            entity.Property(e => e.ConsommateurPk).HasColumnName("Consommateur_PK");
            entity.Property(e => e.ClefSecrete)
                .HasMaxLength(64)
                .IsUnicode(false)
                .HasColumnName("Clef_Secrete");

            entity.HasMany(d => d.CaPkApiFks).WithMany(p => p.CaPkConsommateurFks)
                .UsingEntity<Dictionary<string, object>>(
                    "ConsommateurApi",
                    r => r.HasOne<Api>().WithMany()
                        .HasForeignKey("CaPkApiFk")
                        .OnDelete(DeleteBehavior.ClientSetNull)
                        .HasConstraintName("FK_CA_Api"),
                    l => l.HasOne<Consommateur>().WithMany()
                        .HasForeignKey("CaPkConsommateurFk")
                        .OnDelete(DeleteBehavior.ClientSetNull)
                        .HasConstraintName("FK_CA_Consommateur"),
                    j =>
                    {
                        j.HasKey("CaPkConsommateurFk", "CaPkApiFk");
                        j.ToTable("Consommateur_Api");
                        j.IndexerProperty<int>("CaPkConsommateurFk").HasColumnName("CA_PK_Consommateur_FK");
                        j.IndexerProperty<int>("CaPkApiFk").HasColumnName("CA_PK_Api_FK");
                    });
        });

        modelBuilder.Entity<Jeton>(entity =>
        {
            entity.HasKey(e => e.JetonPk);

            entity.ToTable("Jeton");

            entity.Property(e => e.JetonPk).HasColumnName("Jeton_PK");
            entity.Property(e => e.DateAjout)
                .HasColumnType("datetime")
                .HasColumnName("Date_Ajout");
            entity.Property(e => e.DateGenerationJwt)
                .HasColumnType("datetime")
                .HasColumnName("Date_Generation_Jwt");
            entity.Property(e => e.DateMaj)
                .HasColumnType("datetime")
                .HasColumnName("Date_Maj");
            entity.Property(e => e.Jwt)
                .HasMaxLength(600)
                .IsUnicode(false);
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
