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

namespace Baya.Infrastructure.Persistence.Configuration.BookingConfig;

internal sealed class BookingRequestConfig : IEntityTypeConfiguration<BookingRequest>
{
    public void Configure(EntityTypeBuilder<BookingRequest> builder)
    {
        builder.ToTable("BookingRequests", "booking");

        // required_caregiver_gender is a closed code set (male/female/any); customer_notes is DELIBERATELY
        // unencrypted, limited stage-1 clinical context — never routed through the field encryptor.
        builder.Property(r => r.RequiredCaregiverGender).HasMaxLength(10);
        builder.Property(r => r.CustomerNotes).HasMaxLength(1000);
        builder.Property(r => r.Status).HasMaxLength(50).IsRequired();
        builder.Property(r => r.NurseRejectionReason).HasMaxLength(500);
        builder.Property(r => r.NurseResponseDeadlineAt).IsRequired();

        // Inbox lists read on (party, status), actionable-first; the two deadline indexes let the expiry
        // sweep select stale rows through a covering index instead of scanning the table.
        builder.HasIndex(r => new { r.NurseId, r.Status });
        builder.HasIndex(r => new { r.CustomerId, r.Status });
        builder.HasIndex(r => new { r.Status, r.NurseResponseDeadlineAt });
        builder.HasIndex(r => new { r.Status, r.PaymentDeadlineAt });

        builder.HasOne(r => r.Customer)
            .WithMany()
            .HasForeignKey(r => r.CustomerId)
            .IsRequired();

        builder.HasOne(r => r.Nurse)
            .WithMany()
            .HasForeignKey(r => r.NurseId)
            .IsRequired();

        builder.HasOne(r => r.Patient)
            .WithMany()
            .HasForeignKey(r => r.PatientId)
            .IsRequired();

        builder.HasOne(r => r.Variant)
            .WithMany()
            .HasForeignKey(r => r.VariantId)
            .IsRequired();

        builder.HasOne(r => r.CustomerAddress)
            .WithMany()
            .HasForeignKey(r => r.CustomerAddressId)
            .IsRequired();

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