using Baya.Domain.Entities.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.IdentityConfig;

internal sealed class PatientConfig : IEntityTypeConfiguration<Patient>
{
    public void Configure(EntityTypeBuilder<Patient> builder)
    {
        builder.ToTable("Patients", "usr");

        builder.Property(p => p.DisplayName).HasMaxLength(200);
        builder.Property(p => p.FirstName).HasMaxLength(100);
        builder.Property(p => p.LastName).HasMaxLength(100);
        builder.Property(p => p.Gender).HasMaxLength(10).IsRequired();
        builder.Property(p => p.BloodType).HasMaxLength(10);
        builder.Property(p => p.Relation).HasMaxLength(20);
        builder.Property(p => p.IsActive).HasDefaultValue(true);

        // initial_medical_notes is encrypted at rest (converter wired in ApplicationDbContext).

        // Tenancy anchor: every list/get is scoped by CustomerId.
        builder.HasIndex(p => p.CustomerId);
        builder.HasOne(p => p.Customer)
            .WithMany(c => c.Patients)
            .HasForeignKey(p => p.CustomerId)
            .IsRequired();
    }
}
