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

namespace Infrastructure.Persistence.Configurations.Forum.Logs;

public sealed class PostLinkClickLogConfiguration : IEntityTypeConfiguration<PostLinkClickLog>
{
    public void Configure(EntityTypeBuilder<PostLinkClickLog> builder)
    {
        builder.HasIndex(x => x.PostID);
        builder.HasIndex(x => x.PostLinkID);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => new { x.PostID, x.PostLinkID });
        builder.HasIndex(x => new { x.PostID, x.PostLinkID, x.MemberID });

        builder.HasOne(x => x.Post).WithMany(x => x.PostLinkClickLog).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.PostLink).WithMany().HasForeignKey(x => x.PostLinkID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Restrict);

        builder.ToTable("PostLinkClickLog", 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.PostLinkID).IsRequired().HasComment("게시글 파일 ID");
        builder.Property(x => x.MemberID).HasComment("회원 ID");
        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("등록 일시");
    }
}
