using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Wishlist.Data.Entities;

namespace Wishlist.Data;

public class AppDataContext : IdentityDbContext<User>
{

    public DbSet<Entities.Wishlist> Wishlists { get; set; }
    public DbSet<Wish> Wishes { get; set; }
    public AppDataContext() { }
    
    public AppDataContext(DbContextOptions<AppDataContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<Entities.Wishlist>()
            .HasMany(e => e.Wishes)
            .WithOne(e => e.Wishlist)
            .HasForeignKey(e => e.WishlistId)
            .OnDelete(DeleteBehavior.Cascade)
            .IsRequired();
        
        builder.Entity<User>()
            .HasMany(e => e.Wishlists)
            .WithMany(e => e.Owners)
            .UsingEntity(j => j.ToTable("OwnerWishlists"));
    }
}