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

namespace Infrastructure.Persistence.Configurations.Members;

public class MemberOAuthTokenConfiguration : IEntityTypeConfiguration<MemberOAuthToken>
{
    public void Configure(EntityTypeBuilder<MemberOAuthToken> builder)
    {
        builder.HasOne(x => x.Member).WithMany().HasForeignKey(x => x.MemberID).OnDelete(DeleteBehavior.Cascade);
        builder.HasIndex(x => new { x.MemberID, x.Provider }).IsUnique();

        builder.ToTable(nameof(MemberOAuthToken), x => x.HasComment("회원 OAuth 토큰"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.Provider).HasMaxLength(32).IsRequired();
        builder.Property(x => x.AccessTokenEnc).HasMaxLength(2048).IsRequired();
        builder.Property(x => x.RefreshTokenEnc).HasMaxLength(2048);
        builder.Property(x => x.Scopes).HasMaxLength(1024);
        builder.Property(x => x.ExpiresAt).IsRequired();
        builder.Property(x => x.CreatedAt).IsRequired();
        builder.Property(x => x.UpdatedAt).IsRequired();
    }
}