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

namespace Baya.Infrastructure.Persistence.Configuration.ReviewsConfig;

/// <summary>
/// <c>patient_care_records</c> — nurse-authored, encrypted, <b>patient-scoped</b> clinical notes. The
/// <c>(patient_id, recorded_at DESC)</c> index serves the longitudinal history read. <c>booking_id</c> is
/// nullable provenance only (which visit produced the note) — the scoping key is <c>patient_id</c>.
/// <c>body_encrypted</c> stores <c>IFieldEncryptor</c> ciphertext (no EF value converter — the handler
/// encrypts/decrypts explicitly, so no query path can surface plaintext).
/// </summary>
internal sealed class PatientCareRecordConfig : IEntityTypeConfiguration<PatientCareRecord>
{
    public void Configure(EntityTypeBuilder<PatientCareRecord> builder)
    {
        builder.ToTable("PatientCareRecords", "reviews");

        builder.Property(r => r.BodyEncrypted).IsRequired();
        builder.Property(r => r.RecordedAt).IsRequired();

        builder.HasIndex(r => new { r.PatientId, r.RecordedAt })
            .HasDatabaseName("IX_PatientCareRecords_Patient_RecordedAt");

        builder.HasOne<Patient>().WithMany().HasForeignKey(r => r.PatientId).IsRequired();
        builder.HasOne<NurseProfile>().WithMany().HasForeignKey(r => r.NurseProfileId).IsRequired();
        builder.HasOne<Booking>().WithMany().HasForeignKey(r => r.BookingId);

        builder.HasQueryFilter(r => r.DeletedAt == null);
    }
}
