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

namespace Infrastructure.Persistence.Configurations.Forum.Comments;

public sealed class CommentReportConfiguration : IEntityTypeConfiguration<CommentReport>
{
    public void Configure(EntityTypeBuilder<CommentReport> builder)
    {
        builder.HasIndex(x => x.BoardID);
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.CommentID);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => x.Type);
        builder.HasIndex(x => x.Status);
        builder.HasIndex(x => x.CreatedAt);
        builder.HasIndex(x => new { x.CommentID, x.ID });
        builder.HasIndex(x => new { x.CommentID, x.MemberID }).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.CommentReport).HasForeignKey(x => x.CommentID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable("CommentReport", 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.Type).HasConversion<byte>().IsRequired().HasComment("신고 사유");
        builder.Property(x => x.Reason).HasMaxLength(1000).HasComment("신고 내용");
        builder.Property(x => x.Status).HasConversion<byte>().IsRequired().HasComment("처리 상태");
        builder.Property(x => x.Memo).HasMaxLength(1000).HasComment("처리 내용");
        builder.Property(x => x.IpAddress).HasMaxLength(50).HasComment("IP Address");
        builder.Property(x => x.UserAgent).HasMaxLength(255).HasComment("User-agent");
        builder.Property(x => x.UpdatedAt).HasComment("수정 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}
