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

namespace Infrastructure.Persistence.Configurations.Forum.Comments;

public sealed class CommentMentionConfiguration : IEntityTypeConfiguration<CommentMention>
{
    public void Configure(EntityTypeBuilder<CommentMention> builder)
    {
        builder.HasIndex(x => x.BoardID);
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => x.CommentID).IsUnique();
        builder.HasIndex(x => new { x.CommentID, x.ID });
        builder.HasIndex(x => new { x.CommentID, x.MemberID, x.ID });
        builder.HasIndex(x => new { x.CommentID, x.MemberID, x.Start });

        builder.HasOne(x => x.Board).WithMany().HasForeignKey(x => x.BoardID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.Post).WithMany().HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.Comment).WithOne(x => x.CommentMention).HasForeignKey<CommentMention>(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable("CommentMention", 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.CommentID).IsRequired().HasComment("댓글 ID");
        builder.Property(x => x.MemberID).IsRequired().HasComment("언급된 회원 ID");
        builder.Property(x => x.RawHandle).HasMaxLength(64).IsRequired().HasComment("원문 회원 언급값");
        builder.Property(x => x.Start).IsRequired().HasComment("본문 내 시작 인덱스");
        builder.Property(x => x.Length).IsRequired().HasComment("길이");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}
