using Entities;
using Microsoft.EntityFrameworkCore;

namespace DbConnectionLibrairie
{
    public class MyDbContext : DbContext
    {
        public DbSet<AdministratorEntity> Administrators { get; set; }
        public DbSet<AnswerEntity> Answers { get; set; }
        public DbSet<ChapterEntity> Chapters { get; set; }
        public DbSet<LobbyEntity> Lobbies { get; set; }
        public DbSet<PlayerEntity> Players { get; set; }
        public DbSet<QuestionEntity> Questions { get; set; }
        public DbSet<LobbyEntityPlayerEntity> Use { get; set; }
        public DbSet<PlayerEntityChapterEntity> Play { get; set; }
        public MyDbContext()
        { }

        public MyDbContext(DbContextOptions<MyDbContext> contextOptions) :
            base(contextOptions)
        { }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            if (!options.IsConfigured)
            {
                options.UseSqlite(@"Data source = database.db");
            }
        }

        /// <summary>
        /// function overrided OnModelCreating :
        /// initialise composed primary keys
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LobbyEntityPlayerEntity>().HasKey(u => new { u.IdLobby, u.IdPlayer });
            modelBuilder.Entity<PlayerEntityChapterEntity>().HasKey(u => new { u.IdPlayer, u.IdChapter });

            base.OnModelCreating(modelBuilder);
        }
    }
}
