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

namespace Infrastructure.Persistence.Configurations.Members.Logs;

public sealed class MemberLoginLogConfiguration : IEntityTypeConfiguration<MemberLoginLog>
{
    public void Configure(EntityTypeBuilder<MemberLoginLog> builder)
    {
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.SetNull);
        builder.HasIndex(x => x.MemberID);
        builder.HasIndex(x => x.Account);
        builder.HasIndex(x => new { x.MemberID, x.Success });

        builder.ToTable(nameof(MemberLoginLog), x => x.HasComment("로그인 기록"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.MemberID).HasComment("회원 ID");
        builder.Property(x => x.Success).IsRequired().HasComment("로그인 성공 여부 (0: 실패, 1: 성공)");
        builder.Property(x => x.Account).HasMaxLength(120).IsRequired().HasComment("로그인 시도한 계정");
        builder.Property(x => x.Reason).HasMaxLength(225).HasComment("실패 이유");
        builder.Property(x => x.Referer).HasComment("이전 페이지 주소");
        builder.Property(x => x.Url).HasMaxLength(500).HasComment("요청 주소");
        builder.Property(x => x.IpAddress).HasMaxLength(45).HasComment("IP Address");
        builder.Property(x => x.UserAgent).HasMaxLength(512).HasComment("User Agent");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}