// <copyright file="WebzineDbContext.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace Webzine.EntitiesContext
{
    using Microsoft.EntityFrameworkCore;
    using Webzine.Entity;

    public class WebzineDbContext : DbContext
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebzineDbContext"/> class.
        /// </summary>
        /// <param name="options">Options.</param>
        public WebzineDbContext(DbContextOptions<WebzineDbContext> options)
            : base(options) { }

        /// <summary>
        /// Gets Obtient les artistes de la base.
        /// </summary>
        public DbSet<Artiste> Artistes => this.Set<Artiste>();

        /// <summary>
        /// Gets Obtient les styles de la base.
        /// </summary>
        public DbSet<Style> Styles => this.Set<Style>();

        /// <summary>
        /// Gets obtient les titres de la base.
        /// </summary>
        public DbSet<Titre> Titres => this.Set<Titre>();

        /// <summary>
        /// Gets obtient les commentaires de la base.
        /// </summary>
        public DbSet<Commentaire> Commentaires => this.Set<Commentaire>();

        /// <inheritdoc/>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Artiste>(entity =>
            {
                entity.HasKey(a => a.IdArtiste)
                    .HasName("PK_Artiste");

                entity.HasMany(a => a.Titres)
                    .WithOne(t => t.Artiste)
                    .HasForeignKey(t => t.IdArtiste)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<Style>(entity =>
            {
                entity.HasKey(s => s.IdStyle)
                    .HasName("PK_Style");
            });

            modelBuilder.Entity<Commentaire>(entity =>
            {
                entity.HasKey(c => c.IdCommentaire)
                    .HasName("PK_Commentaire");

                entity.HasOne(c => c.Titre)
                    .WithMany(t => t.Commentaires)
                    .HasForeignKey(c => c.IdTitre)
                    .OnDelete(DeleteBehavior.Cascade);
            });

            modelBuilder.Entity<Titre>(entity =>
            {
                entity.HasKey(t => t.IdTitre)
                    .HasName("PK_Titre");

                entity.HasMany(t => t.Styles)
                    .WithMany(s => s.Titres)
                    .UsingEntity<Dictionary<string, object>>(
                        "TitreStyle",
                        right => right.HasOne<Style>()
                            .WithMany()
                            .HasForeignKey("IdStyle")
                            .OnDelete(DeleteBehavior.Cascade),
                        left => left.HasOne<Titre>()
                            .WithMany()
                            .HasForeignKey("IdTitre")
                            .OnDelete(DeleteBehavior.Cascade),
                        join =>
                        {
                            join.HasKey("IdTitre", "IdStyle");
                            join.ToTable("TitreStyle");
                        });
            });
        }
    }
}
