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

namespace Infrastructure.Persistence.Configurations.Forum.Logs;

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

        builder.HasOne(x => x.Post).WithMany(x => x.PostFileDownLog).HasForeignKey(x => x.PostID).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(x => x.PostFile).WithMany().HasForeignKey(x => x.PostFileID).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Restrict);

        builder.ToTable("PostFileDownLog", 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.PostFileID).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("등록 일시");
    }
}
