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

namespace Infrastructure.Persistence.Configurations.Forum.Comments;

public sealed class CommentImageConfiguration : IEntityTypeConfiguration<CommentImage>
{
    public void Configure(EntityTypeBuilder<CommentImage> builder)
    {
        builder.HasIndex(x => x.BoardID);
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.CommentID);
        builder.HasIndex(x => x.UUID).IsUnique();

        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).WithMany(x => x.CommentImage).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable("CommentImage", x => x.HasComment("댓글 이미지"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
    }
}
