using Lada.Framework.Data.Domains.Trucks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Trucks;

public class FuelQuotaPaymentMap : IEntityMappingConfiguration<FuelQuotaPayment>
{
    public void Map(EntityTypeBuilder<FuelQuotaPayment> builder)
    {
        builder.ToTable("FuelQuotaPayments");

        // Primary key
        builder.HasKey(x => x.Id);

        // Properties
        builder.Property(x => x.DriverId)
            .IsRequired();

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

        builder.Property(x => x.PaidLiters)
            .HasPrecision(18, 4)
            .IsRequired();

        builder.Property(x => x.FuelUnitPrice)
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(x => x.PaidAmount)
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(x => x.BalanceBeforeLiters)
            .HasPrecision(18, 4)
            .IsRequired();

        builder.Property(x => x.BalanceAfterLiters)
            .HasPrecision(18, 4)
            .IsRequired();

        builder.Property(x => x.PaymentMethod)
            .HasMaxLength(100)
            .HasDefaultValue(string.Empty);

        builder.Property(x => x.PaymentReference)
            .HasMaxLength(256)
            .HasDefaultValue(string.Empty);

        builder.Property(x => x.Notes)
            .HasMaxLength(1024)
            .HasDefaultValue(string.Empty);

        builder.Property(x => x.ApprovedByUserName)
            .HasMaxLength(256)
            .HasDefaultValue(string.Empty);

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

        // Relationships
        builder.HasOne(x => x.Driver)
            .WithMany()
            .HasForeignKey(x => x.DriverId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(x => x.ApprovedByUser)
            .WithMany()
            .HasForeignKey(x => x.ApprovedByUserId)
            .OnDelete(DeleteBehavior.SetNull);

        // Indexes
        builder.HasIndex(x => x.DriverId)
            .HasDatabaseName("IX_FuelQuotaPayments_DriverId");

        builder.HasIndex(x => x.PaymentDate)
            .HasDatabaseName("IX_FuelQuotaPayments_PaymentDate");

        builder.HasIndex(x => x.Status)
            .HasDatabaseName("IX_FuelQuotaPayments_Status");

        builder.HasIndex(x => x.ApprovedByUserId)
            .HasDatabaseName("IX_FuelQuotaPayments_ApprovedByUserId");

        builder.HasIndex(x => new { x.DriverId, x.PaymentDate })
            .HasDatabaseName("IX_FuelQuotaPayments_DriverId_PaymentDate");
    }
}
