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

namespace WorkClub.Infrastructure.Data.Configurations;

public class MemberConfiguration : IEntityTypeConfiguration<Member>
{
    public void Configure(EntityTypeBuilder<Member> builder)
    {
        builder.ToTable("members");

        builder.HasKey(m => m.Id);

        builder.Property(m => m.TenantId)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(m => m.ExternalUserId)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(m => m.DisplayName)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(m => m.Email)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(m => m.Role)
            .IsRequired()
            .HasConversion<int>();

        builder.Property(m => m.ClubId)
            .IsRequired();

        builder.Property(m => m.CreatedAt)
            .IsRequired();

        builder.Property(m => m.UpdatedAt)
            .IsRequired();

        builder.HasIndex(m => m.TenantId)
            .HasDatabaseName("ix_members_tenant_id");

        builder.HasIndex(m => m.ClubId)
            .HasDatabaseName("ix_members_club_id");

        builder.HasIndex(m => new { m.TenantId, m.ExternalUserId })
            .HasDatabaseName("ix_members_tenant_external_user");
    }
}
