using CMSMicroservice.Domain.Entities.Payment;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

public class ManualPaymentConfiguration : IEntityTypeConfiguration<ManualPayment>
{
    public void Configure(EntityTypeBuilder<ManualPayment> builder)
    {
        builder.HasQueryFilter(p => !p.IsDeleted);
        builder.ToTable("ManualPayments");

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

        builder.Property(x => x.UserId)
            .IsRequired();

        builder.Property(x => x.Amount)
            .IsRequired();

        builder.Property(x => x.Type)
            .IsRequired();

        builder.Property(x => x.Description)
            .IsRequired()
            .HasMaxLength(1000);

        builder.Property(x => x.ReferenceNumber)
            .HasMaxLength(100);

        builder.Property(x => x.Status)
            .IsRequired();

        builder.Property(x => x.RequestedBy)
            .IsRequired();

        builder.Property(x => x.ApprovedBy);

        builder.Property(x => x.ApprovedAt);

        builder.Property(x => x.RejectionReason)
            .HasMaxLength(500);

        builder.Property(x => x.TransactionId);

        // Relations
        builder.HasOne(x => x.User)
            .WithMany()
            .HasForeignKey(x => x.UserId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(x => x.Transaction)
            .WithMany()
            .HasForeignKey(x => x.TransactionId)
            .OnDelete(DeleteBehavior.Restrict);

        // Indexes
        builder.HasIndex(x => x.UserId);
        builder.HasIndex(x => x.Status);
        builder.HasIndex(x => x.RequestedBy);
        builder.HasIndex(x => x.ApprovedBy);
        builder.HasIndex(x => x.Created);
        builder.HasIndex(x => new { x.UserId, x.Status });
    }
}
