using Baya.Domain.Entities.Identity;
using Baya.Domain.Entities.PartnerCenters;
using Baya.Domain.Entities.User;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Baya.Infrastructure.Persistence.Configuration.PartnerCentersConfig;
///
/// partner_centers — the licensed sponsor center. settlement_iban is encrypted at rest (converter
/// wired in ApplicationDbContext) and masked in reads; commission_rate is the center's own cut
/// (separate from platform_fee_rate). Indexes on is_active (the active-center list) and
/// admin_user_id (the center portal scope). The 1:N sponsorship to nurse_profiles is configured
/// here (adds the nurse_profiles.partner_center_id FK in place, without forking a parallel table).
///
internal sealed class PartnerCenterConfig : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("PartnerCenters", "partner");
builder.Property(c => c.Name).HasMaxLength(300).IsRequired();
builder.Property(c => c.LegalEntityType).HasMaxLength(30);
builder.Property(c => c.MohEstablishmentPermitNo).HasMaxLength(100).IsRequired();
builder.Property(c => c.TechnicalDirectorLicenseNo).HasMaxLength(100);
builder.Property(c => c.EnamadCode).HasMaxLength(100);
builder.Property(c => c.SettlementIban).HasMaxLength(256); // ciphertext is longer than the 34-char plaintext
builder.Property(c => c.CommissionRate).HasPrecision(5, 4);
builder.HasIndex(c => c.IsActive);
builder.HasIndex(c => c.AdminUserId);
builder.HasOne().WithMany().HasForeignKey(c => c.AdminUserId).IsRequired();
builder.HasOne().WithMany().HasForeignKey(c => c.TechnicalDirectorNurseUserId).IsRequired(false);
// 1:N sponsorship — adds nurse_profiles.partner_center_id FK in place (nullable; NULL once Balinyaar
// holds its own permit). No inverse navigation on NurseProfile (kept lean).
builder.HasMany()
.WithOne()
.HasForeignKey(n => n.PartnerCenterId)
.IsRequired(false);
builder.HasQueryFilter(c => c.DeletedAt == null);
}
}