using Domain.Entities.Forum.Comments;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Persistence.Configurations.Forum.Comments;

public sealed class CommentConfiguration : IEntityTypeConfiguration<Comment>
{
    public void Configure(EntityTypeBuilder<Comment> builder)
    {
        builder.HasIndex(x => x.BoardID);
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => x.ParentID);
        builder.HasIndex(x => new { x.PostID, x.IsDeleted, x.ParentID, x.CreatedAt }).IsDescending(false, false, false, true);
        builder.HasIndex(x => new { x.PostID, x.IsDeleted, x.ParentID, x.Score, x.ID }).IsDescending(false, false, false, true, true);
        builder.HasIndex(x => new { x.PostID, x.MemberID, x.IsDeleted, x.ParentID, x.CreatedAt }).IsDescending(false, false, false, false, true);
        builder.HasIndex(x => new { x.PostID, x.MemberID, x.IsDeleted, x.ParentID, x.Score, x.ID }).IsDescending(false, false, false, false, true, true);
        builder.HasIndex(x => new { x.PostID, x.IsDeleted, x.ParentID, x.CreatedAt });
        builder.HasIndex(x => new { x.PostID, x.MemberID, x.IsDeleted, x.ParentID, x.CreatedAt });

        builder.HasOne(x => x.Board).WithMany().HasForeignKey(x => x.BoardID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.Post).WithMany(x => x.Comment).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.MentionMember).WithMany().HasForeignKey(x => x.MentionMemberID).OnDelete(DeleteBehavior.SetNull);
        builder.HasOne(x => x.Parent).WithMany(x => x.Children).HasForeignKey(x => x.ParentID).OnDelete(DeleteBehavior.Restrict);
        builder.HasMany(x => x.CommentImage).WithOne(x => x.Comment).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.CommentMedia).WithOne(x => x.Comment).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.CommentFile).WithOne(x => x.Comment).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.CommentLink).WithOne(x => x.Comment).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.CommentReaction).WithOne(x => x.Comment).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.CommentReport).WithOne(x => x.Comment).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.CommentMention).WithOne(x => x.Comment).HasForeignKey<CommentMention>(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable(nameof(Comment), x => x.HasComment("댓글"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.BoardID).IsRequired().HasComment("게시판 ID");
        builder.Property(x => x.PostID).IsRequired().HasComment("게시글 ID");
        builder.Property(x => x.MemberID).IsRequired().HasComment("회원 ID");
        builder.Property(x => x.ParentID).HasComment("부모 댓글 ID");
        builder.Property(x => x.MentionMemberID).HasComment("언급 대상 회원 ID");
        builder.Property(x => x.Depth).IsRequired().HasComment("댓글 깊이");
        builder.Property(x => x.Content).HasMaxLength(4000).IsRequired().HasComment("댓글 내용");
        builder.Property(x => x.Status).HasConversion<byte>().HasComment("댓글 상태");
        builder.Property(x => x.IsDeleted).IsRequired().HasComment("삭제 여부");
        builder.Property(x => x.Score).IsRequired().HasComment("점수");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}
