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

namespace Baya.Infrastructure.Persistence.Configuration.PaymentsConfig;

/// <summary>
/// <c>ledger_entries</c> is <b>append-only</b>: no soft-delete, no audit-modified columns, no query filter,
/// and (because the entity implements <c>IEntity</c> only, not <c>ITimeModification</c>) the audit interceptor
/// never stamps a modify on it. Corrections are new balancing rows, never edits.
/// </summary>
internal sealed class LedgerEntryConfig : IEntityTypeConfiguration<LedgerEntry>
{
    public void Configure(EntityTypeBuilder<LedgerEntry> builder)
    {
        builder.ToTable("LedgerEntries", "payments");

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

        builder.Property(e => e.AccountType).HasMaxLength(40).IsRequired();
        builder.Property(e => e.Direction).HasMaxLength(6).IsRequired();
        builder.Property(e => e.SourceRefType).HasMaxLength(40).IsRequired();
        builder.Property(e => e.Memo).HasMaxLength(300);

        // Balance reads (b13 payouts): the nurse_payable balance for a nurse; a posting group; a source's legs.
        builder.HasIndex(e => new { e.AccountType, e.NurseId });
        builder.HasIndex(e => e.TransactionGroupId);
        builder.HasIndex(e => new { e.SourceRefType, e.SourceRefId });
        builder.HasIndex(e => e.BookingId);

        builder.HasOne<NurseProfile>().WithMany().HasForeignKey(e => e.NurseId).IsRequired(false);
        builder.HasOne<Booking>().WithMany().HasForeignKey(e => e.BookingId).IsRequired(false);
    }
}
