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.WidgetToken).HasMaxLength(32).IsRequired().HasComment("위젯 토큰");
        builder.HasIndex(x => x.WidgetToken).IsUnique();
        builder.Property(x => x.UpdatedAt).HasComment("���� �Ͻ�");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("��� �Ͻ�");
    }
}