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

namespace Infrastructure.Persistence.Configurations.Members;

public sealed class ChannelConfiguration : IEntityTypeConfiguration<Channel>
{
    public void Configure(EntityTypeBuilder<Channel> builder)
    {
        builder.HasOne(x => x.Member).WithOne(x => x.Channel).HasForeignKey<Channel>(x => x.MemberID).OnDelete(DeleteBehavior.Cascade);
        builder.HasIndex(x => x.MemberID).IsUnique();
        builder.HasIndex(x => x.SID).IsUnique();
        builder.HasIndex(x => x.Name).IsUnique();
        builder.HasIndex(x => x.Handle).IsUnique();
        builder.HasIndex(x => x.YouTubeUrl).IsUnique();
        builder.HasIndex(x => new { x.MemberID, x.IsVerified });
        builder.HasIndex(x => new { x.MemberID, x.IsActive });
        builder.HasIndex(x => new { x.MemberID, x.IsVerified, x.IsActive });

        builder.ToTable(nameof(Channel), x => x.HasComment("채널 정보"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd();
        builder.Property(x => x.MemberID).IsRequired().HasComment("회원 ID");
        builder.Property(x => x.SID).HasMaxLength(24).IsRequired().HasComment("채널 ID");
        builder.Property(x => x.Name).HasMaxLength(200).IsRequired().HasComment("채널 이름");
        builder.Property(x => x.Handle).HasMaxLength(30).HasComment("핸들");
        builder.Property(x => x.YouTubeUrl).HasMaxLength(255).IsRequired().HasComment("YouTube 채널 URL");
        builder.Property(x => x.PlatformFeeRate).HasPrecision(5, 2).IsRequired().HasComment("수수료(%)");
        builder.Property(x => x.IsVerified).IsRequired().HasComment("인증 여부");
        builder.Property(x => x.IsActive).IsRequired().HasComment("활성 여부");
        builder.Property(x => x.UpdatedAt).HasComment("수정 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}