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

namespace Infrastructure.Persistence.Configurations.Forum.Logs;

public sealed class PostUpdateLogConfiguration : IEntityTypeConfiguration<PostUpdateLog>
{
    public void Configure(EntityTypeBuilder<PostUpdateLog> builder)
    {
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => x.SubjectDiff);
        builder.HasIndex(x => x.ContentDiff);
        builder.HasIndex(x => new { x.PostID, x.ID });
        builder.HasIndex(x => new { x.PostID, x.MemberID });
        builder.HasIndex(x => new { x.PostID, x.MemberID, x.ID });

        builder.HasOne(x => x.Post).WithMany(x => x.PostUpdateLog).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable("PostUpdateLog", x => x.HasComment("게시글 수정 로그"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.PostID).IsRequired().HasComment("게시판 ID");
        builder.Property(x => x.MemberID).IsRequired().HasComment("회원 ID");
        builder.Property(x => x.SubjectDiff).HasMaxLength(4000).HasComment("변경 제목");
        builder.Property(x => x.ContentDiff).HasMaxLength(4000).HasComment("변경 내용");
        builder.Property(x => x.Note).HasMaxLength(200).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.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}
