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

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

public class FuelCardTransactionMap : IEntityMappingConfiguration<FuelCardTransaction>
{
    public void Map(EntityTypeBuilder<FuelCardTransaction> builder)
    {
        builder.ToTable("FuelCardTransactions");

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

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

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

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

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

        builder.Property(x => x.Currency)
            .HasMaxLength(10)
            .HasDefaultValue("VND");

        builder.Property(x => x.TransactionType)
            .HasMaxLength(50)
            .HasDefaultValue(string.Empty);

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

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

        builder.Property(x => x.RawData)
            .HasMaxLength(4000)
            .HasDefaultValue(string.Empty);

        builder.Property(x => x.IsReconciled)
            .HasDefaultValue(false);

        builder.Property(x => x.IsPaidToBank)
            .HasDefaultValue(false);

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

        // Relationships
        builder.HasOne(x => x.FuelCard)
            .WithMany(x => x.FuelCardTransactions)
            .HasForeignKey(x => x.FuelCardId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(x => x.FuelCardStatementImport)
            .WithMany(x => x.FuelCardTransactions)
            .HasForeignKey(x => x.ImportId)
            .OnDelete(DeleteBehavior.SetNull);

        // Note: Relationship with FuelRefill is defined in FuelRefillMap (one-to-one)

        // Indexes
        builder.HasIndex(x => x.FuelCardId)
            .HasDatabaseName("IX_FuelCardTransactions_FuelCardId");

        builder.HasIndex(x => x.TransactionDate)
            .HasDatabaseName("IX_FuelCardTransactions_TransactionDate");

        builder.HasIndex(x => x.ImportId)
            .HasDatabaseName("IX_FuelCardTransactions_ImportId");

        builder.HasIndex(x => x.FuelRefillId)
            .HasDatabaseName("IX_FuelCardTransactions_FuelRefillId");

        builder.HasIndex(x => x.IsReconciled)
            .HasDatabaseName("IX_FuelCardTransactions_IsReconciled");

        builder.HasIndex(x => x.IsPaidToBank)
            .HasDatabaseName("IX_FuelCardTransactions_IsPaidToBank");

        builder.HasIndex(x => new { x.FuelCardId, x.TransactionDate })
            .HasDatabaseName("IX_FuelCardTransactions_FuelCardId_TransactionDate");
    }
}
