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

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

public class FuelRefillMap : IEntityMappingConfiguration<FuelRefill>
{
    public void Map(EntityTypeBuilder<FuelRefill> builder)
    {
        builder.ToTable("FuelRefills");

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

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

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

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

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

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

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

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

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

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

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

        builder.Property(x => x.StationAddress)
            .HasMaxLength(512)
            .HasDefaultValue(string.Empty);

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

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

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

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

        builder.HasOne(x => x.FuelCard)
            .WithMany()
            .HasForeignKey(x => x.FuelCardId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasOne(x => x.FuelCardTransaction)
            .WithOne(x => x.FuelRefill)
            .HasForeignKey<FuelRefill>(x => x.FuelCardTransactionId)
            .OnDelete(DeleteBehavior.SetNull);

        // Indexes
        builder.HasIndex(x => x.TruckId)
            .HasDatabaseName("IX_FuelRefills_TruckId");

        builder.HasIndex(x => x.DriverId)
            .HasDatabaseName("IX_FuelRefills_DriverId");

        builder.HasIndex(x => x.FuelCardId)
            .HasDatabaseName("IX_FuelRefills_FuelCardId");

        builder.HasIndex(x => x.RefillTime)
            .HasDatabaseName("IX_FuelRefills_RefillTime");

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

        builder.HasIndex(x => new { x.TruckId, x.RefillTime })
            .HasDatabaseName("IX_FuelRefills_TruckId_RefillTime");
    }
}
