using Microsoft.EntityFrameworkCore;
using ReAct_PME.Domain;
using ReAct_PME.Infrastructure.Persistence.Configurations;


namespace ReAct_PME.Infrastructure.Persistence
{
    public partial class AppDbContext : DbContext
    {
		#region Les DbSet
		public DbSet<DT_CONVERSATION> DT_CONVERSATION { get; set; }
		public DbSet<DT_CONVERSATION_LIGNE> DT_CONVERSATION_LIGNE { get; set; }
		public DbSet<DT_LOGS> DT_LOGS { get; set; }
		public DbSet<DT_ROLE> DT_ROLE { get; set; }
		public DbSet<DT_WORK_FORCE> DT_WORK_FORCE { get; set; }
		public DbSet<DT_WORK_FORCE_ROLE> DT_WORK_FORCE_ROLE { get; set; }
		#endregion


		#region Constructeur
		public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) {}
		#endregion

		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			#region Les Configurations
			modelBuilder.ApplyConfiguration(new DT_CONVERSATIONConfiguration());
			modelBuilder.ApplyConfiguration(new DT_CONVERSATION_LIGNEConfiguration());
			modelBuilder.ApplyConfiguration(new DT_LOGSConfiguration());
			modelBuilder.ApplyConfiguration(new DT_ROLEConfiguration());
			modelBuilder.ApplyConfiguration(new DT_WORK_FORCEConfiguration());
			modelBuilder.ApplyConfiguration(new DT_WORK_FORCE_ROLEConfiguration());
			#endregion

			#region Les champs des tables
			modelBuilder.Entity<DT_CONVERSATION>(entity => {
				entity.HasKey(e => e.ID_CONVERSATION); // Clé primaire
				entity.Property(e => e.ID_CONVERSATION)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.ID_WORK_FORCE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.DATE_START)
				.HasColumnType("date")
				.IsRequired(true);
				entity.Property(e => e.LIBELLE)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.SystemDateCreated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.SystemDateUpdated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.IsEnabled)
				.HasColumnType("bit")
				.IsRequired(true);
                entity.Property(e => e.TypeConv)
				.HasColumnType("nvarchar(5)")
				.IsRequired(false);
            });

			modelBuilder.Entity<DT_CONVERSATION_LIGNE>(entity => {
				entity.HasKey(e => e.ID_CONVERSATION_LIGNE); // Clé primaire
				entity.Property(e => e.ID_CONVERSATION_LIGNE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.ID_CONVERSATION)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.DATE)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.QUESTION)
				.HasColumnType("nvarchar(max)")
				.IsRequired(true);
				entity.Property(e => e.REPONSE)
				.HasColumnType("nvarchar(max)")
				.IsRequired(true);
				entity.Property(e => e.MODELE)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.SystemDateCreated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.SystemDateUpdated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.IsEnabled)
				.HasColumnType("bit")
				.IsRequired(true);
                entity.Property(e => e.DomainRAG)
				.HasColumnType("nvarchar(50)")
				.IsRequired(false);
            });

			modelBuilder.Entity<DT_LOGS>(entity => {
				entity.HasKey(e => e.ID); // Clé primaire
				entity.Property(e => e.ID)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.ID_WORK_FORCE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.NIVEAU)
				.HasColumnType("nvarchar(50)")
				.IsRequired(true);
				entity.Property(e => e.HORODATAGE)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.MESSAGE)
				.HasColumnType("nvarchar(max)")
				.IsRequired(true);
			});

			modelBuilder.Entity<DT_ROLE>(entity => {
				entity.HasKey(e => e.ID_ROLE); // Clé primaire
				entity.Property(e => e.ID_ROLE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.DATE_START)
				.HasColumnType("date")
				.IsRequired(true);
				entity.Property(e => e.DATE_END)
				.HasColumnType("date")
				.IsRequired(true);
				entity.Property(e => e.LIBELLE_ROLE)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.DESCRIPTION_ROLE)
				.HasColumnType("nvarchar(max)")
				.IsRequired(true);
				entity.Property(e => e.SystemDateCreated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.SystemDateUpdated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.IsEnabled)
				.HasColumnType("bit")
				.IsRequired(true);
			});

			modelBuilder.Entity<DT_WORK_FORCE>(entity => {
				entity.HasKey(e => e.ID_WORK_FORCE); // Clé primaire
				entity.Property(e => e.ID_WORK_FORCE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.TRIGRAMME)
				.HasColumnType("nvarchar(3)")
				.IsRequired(true);
				entity.Property(e => e.LAST_NAME)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.FIRST_NAME)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.EMAIL)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.IsManager)
				.HasColumnType("bit")
				.IsRequired(true);
				entity.Property(e => e.USERNAME)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.PASSWORD)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.MAIL_PASSWORD)
				.HasColumnType("nvarchar(255)")
				.IsRequired(false);
				entity.Property(e => e.MAIL_Serveur_IMAP)
				.HasColumnType("nvarchar(255)")
				.IsRequired(false);
				entity.Property(e => e.MAIL_Serveur_IMAP_PORT)
				.HasColumnType("int")
				.IsRequired(false);
				entity.Property(e => e.MAIL_Serveur_SMTP)
				.HasColumnType("nvarchar(255)")
				.IsRequired(false);
				entity.Property(e => e.MAIL_Serveur_SMTP_PORT)
				.HasColumnType("int")
				.IsRequired(false);
				entity.Property(e => e.MAIL_IS_OK)
				.HasColumnType("bit")
				.IsRequired(false);
				entity.Property(e => e.MAIL_ROLE_SIGNATURE)
				.HasColumnType("nvarchar(255)")
				.IsRequired(false);
				entity.Property(e => e.MAIL_ENTREPRISE_SIGNATURE)
				.HasColumnType("nvarchar(255)")
				.IsRequired(false);
				entity.Property(e => e.MAIL_DelayRefresh)
				.HasColumnType("int")
				.IsRequired(false);
				entity.Property(e => e.MAIL_DelaySentRefresh)
				.HasColumnType("int")
				.IsRequired(false);
				entity.Property(e => e.MAIL_DelaySentRecup)
				.HasColumnType("int")
				.IsRequired(false);
				entity.Property(e => e.MAIL_OverdueDaysSent)
				.HasColumnType("int")
				.IsRequired(false);
				entity.Property(e => e.SystemDateCreated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.SystemDateUpdated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.IsEnabled)
				.HasColumnType("bit")
				.IsRequired(true);
			});

			modelBuilder.Entity<DT_WORK_FORCE_ROLE>(entity => {
				entity.HasKey(e => e.ID_WORK_FORCE_ROLE); // Clé primaire
				entity.Property(e => e.ID_WORK_FORCE_ROLE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.ID_WORK_FORCE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.ID_ROLE)
				.HasColumnType("uniqueidentifier")
				.IsRequired(true);
				entity.Property(e => e.DATE_START)
				.HasColumnType("date")
				.IsRequired(true);
				entity.Property(e => e.DATE_END)
				.HasColumnType("date")
				.IsRequired(true);
				entity.Property(e => e.LIBELLE_WORK_FORCE_ROLE)
				.HasColumnType("nvarchar(255)")
				.IsRequired(true);
				entity.Property(e => e.DESCRIPTION_WORK_FORCE_ROLE)
				.HasColumnType("nvarchar(max)")
				.IsRequired(true);
				entity.Property(e => e.SystemDateCreated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.SystemDateUpdated)
				.HasColumnType("datetime2")
				.IsRequired(true);
				entity.Property(e => e.IsEnabled)
				.HasColumnType("bit")
				.IsRequired(true);
			});

			#endregion

			#region Initialisation de données
			/*
			modelBuilder.Entity<DT_TABLE>().HasData(
				new DT_TABLE
				{
					ID = Guid.NewGuid(),
					LIBELLE = "Libellé 1",
				},
				new DT_TABLE
				{
					ID = Guid.NewGuid(),
					LIBELLE = "Libellé 2",
				}
			);
			*/
			#endregion

			#region Les liens de navigation
			// Pour DT_CONVERSATION

			modelBuilder.Entity<DT_CONVERSATION>()
				.HasOne(c => c.DT_WORK_FORCE) // Propriété de navigation
				.WithMany() // Pas de relation inverse
				.HasForeignKey(c => c.ID_WORK_FORCE) // Clé étrangère
				.OnDelete(DeleteBehavior.Restrict); // Pas de suppression en cascade

			// Pour DT_CONVERSATION_LIGNE

			modelBuilder.Entity<DT_CONVERSATION_LIGNE>()
				.HasOne(c => c.DT_CONVERSATION) // Propriété de navigation
				.WithMany() // Pas de relation inverse
				.HasForeignKey(c => c.ID_CONVERSATION) // Clé étrangère
				.OnDelete(DeleteBehavior.Restrict); // Pas de suppression en cascade

			// Pour DT_LOGS

			modelBuilder.Entity<DT_LOGS>()
				.HasOne(c => c.DT_WORK_FORCE) // Propriété de navigation
				.WithMany() // Pas de relation inverse
				.HasForeignKey(c => c.ID_WORK_FORCE) // Clé étrangère
				.OnDelete(DeleteBehavior.Restrict); // Pas de suppression en cascade

			// Pour DT_WORK_FORCE_ROLE

			modelBuilder.Entity<DT_WORK_FORCE_ROLE>()
				.HasOne(c => c.DT_WORK_FORCE) // Propriété de navigation
				.WithMany() // Pas de relation inverse
				.HasForeignKey(c => c.ID_WORK_FORCE) // Clé étrangère
				.OnDelete(DeleteBehavior.Restrict); // Pas de suppression en cascade

			modelBuilder.Entity<DT_WORK_FORCE_ROLE>()
				.HasOne(c => c.DT_ROLE) // Propriété de navigation
				.WithMany() // Pas de relation inverse
				.HasForeignKey(c => c.ID_ROLE) // Clé étrangère
				.OnDelete(DeleteBehavior.Restrict); // Pas de suppression en cascade

			#endregion

			base.OnModelCreating(modelBuilder);
		}


    }
}
