namespace Kalaha.Api.Database
{
    using Kalaha.Api.Database.Models;
    using Microsoft.EntityFrameworkCore;

    public class KalahaDbContext : DbContext
    {
        public KalahaDbContext(DbContextOptions<KalahaDbContext> options) : base(options) { }

        public DbSet<User> Users { get; set; }
        public DbSet<Game> Games { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Verhindert Abstürze durch mehrfache Relationen zur selben Tabelle
            modelBuilder.Entity<Game>()
                .HasOne(g => g.Player1)
                .WithMany()
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Game>()
                .HasOne(g => g.Player2)
                .WithMany()
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Game>()
                .HasOne(g => g.ActivePlayer)
                .WithMany()
                .OnDelete(DeleteBehavior.Restrict);
        }
    }

    
}
