using Microsoft.EntityFrameworkCore;
using bitforum.Models;
using bitforum.Models.Page;
using bitforum.Models.Page.Faq;
using bitforum.Models.Page.Banner;
using bitforum.Models.Account;
using bitforum.Models.Log;
using bitforum.Models.BBS;

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

    // 공통 사항
    public DbSet<Config> Config { get; set; }
    public DbSet<Document> Document { get; set; }
    public DbSet<FaqCategory> FaqCategory { get; set; }
    public DbSet<FaqItem> FaqItem { get; set; }
    public DbSet<Popup> Popup { get; set; }
    public DbSet<BannerPosition> BannerPosition { get; set; }
    public DbSet<BannerItem> BannerItem { get; set; }

    // 회원
    public DbSet<Member> Member { get; set; }
    public DbSet<MemberApprove> MemberApprove { get; set; }
    public DbSet<MemberGrade> MemberGrade { get; set; }
    public DbSet<RefreshToken> RefreshToken { get; set; }
    public DbSet<EmailVerifyToken> EmailVerifyToken { get; set; }
    public DbSet<EmailVerifyNumber> EmailVerifyNumber { get; set; }
    public DbSet<LoginLog> LoginLog { get; set; }
    public DbSet<EmailLog> EmailLog { get; set; }
    public DbSet<EmailChangeLog> EmailChangeLog { get; set; }
    public DbSet<NameChangeLog> NameChangeLog { get; set; }

    // 게시판
    public DbSet<BoardGroup> BoardGroup { get; set; }
    public DbSet<Board> Board { get; set; }
    public DbSet<BoardMeta> BoardMeta { get; set; }
    public DbSet<Post> Post { get; set; }
    public DbSet<PostMeta> PostMeta { get; set; }
    public DbSet<Comment> Comment { get; set; }
    public DbSet<CommentMeta> CommentMeta { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // 삭제 시 SET NULL 1:N 관계
        modelBuilder.Entity<LoginLog>().HasOne(e => e.Member).WithMany(e => e.LoginLog).HasForeignKey(e => e.MemberID).OnDelete(DeleteBehavior.SetNull);
        modelBuilder.Entity<EmailLog>().HasOne(e => e.Member).WithMany(e => e.EmailLog).HasForeignKey(e => e.MemberID).OnDelete(DeleteBehavior.SetNull);

        // 회원 - 회원 기록들 1:N 관계
        modelBuilder.Entity<EmailChangeLog>().HasOne(e => e.Member).WithMany(e => e.EmailChangeLog).HasForeignKey(e => e.MemberID).OnDelete(DeleteBehavior.NoAction);
        modelBuilder.Entity<NameChangeLog>().HasOne(e => e.Member).WithMany(e => e.NameChangeLog).HasForeignKey(e => e.MemberID).OnDelete(DeleteBehavior.NoAction);

        // 회원 - 회원등급 1:1 관계
        modelBuilder.Entity<MemberApprove>().HasOne(e => e.Member).WithOne(e => e.MemberApprove).HasForeignKey<MemberApprove>(e => e.MemberID).OnDelete(DeleteBehavior.NoAction); // 1:1

        // 게시판 관계 설정
        modelBuilder.Entity<Board>().HasOne(e => e.BoardGroup) .WithMany(e => e.Board).HasForeignKey(e => e.BoardGroupID).OnDelete(DeleteBehavior.NoAction); // 게시판 분류-게시판 관계
        modelBuilder.Entity<Post>().HasOne(e => e.Board).WithMany(e => e.Post).HasForeignKey(e => e.BoardID).OnDelete(DeleteBehavior.NoAction); // 게시판-게시글 관계
        modelBuilder.Entity<Comment>().HasOne(e => e.Post).WithMany(e => e.Comment).HasForeignKey(e => e.PostID).OnDelete(DeleteBehavior.NoAction); // 게시판-댓글 관계
        modelBuilder.Entity<Comment>().HasOne(e => e.Parent).WithMany(e => e.Reply).HasForeignKey(e => e.ParentID).OnDelete(DeleteBehavior.NoAction); // 댓글 부모-자식 관계
    }
}