using ERPCore.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class GrnConfiguration : IEntityTypeConfiguration<Grn>
{
    public void Configure(EntityTypeBuilder<Grn> builder)
    {
        builder.ToTable("grns");
        builder.HasKey(g => g.GrnId);

        builder.Property(g => g.DocNo).IsRequired().HasMaxLength(30);
        builder.HasIndex(g => g.DocNo).IsUnique();

        builder.Property(g => g.Status).HasConversion<string>().HasMaxLength(20).IsRequired();
        builder.Property(g => g.CreatedAt).IsRequired();
        builder.Property(g => g.GlJournalNo).HasMaxLength(30);
        builder.Property(g => g.PaidAmount).HasPrecision(18, 4);
        builder.Property(g => g.BalanceAmount).HasPrecision(18, 4);
        builder.Property(g => g.RowVersion).IsRowVersion();

        builder.HasOne(g => g.PurchaseOrder).WithMany().HasForeignKey(g => g.PoId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(g => g.Vendor).WithMany().HasForeignKey(g => g.VendorId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(g => g.Warehouse).WithMany().HasForeignKey(g => g.WarehouseId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(g => g.Creator).WithMany().HasForeignKey(g => g.CreatedBy).OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(g => g.Status);
        builder.HasIndex(g => g.PoId);
    }
}

public sealed class GrnPaymentConfiguration : IEntityTypeConfiguration<GrnPayment>
{
    public void Configure(EntityTypeBuilder<GrnPayment> builder)
    {
        builder.ToTable("grn_payments");
        builder.HasKey(p => p.GrnPaymentId);

        builder.Property(p => p.Amount).HasPrecision(18, 4);
        builder.Property(p => p.PaymentDate).IsRequired();
        builder.Property(p => p.BankAccountName).IsRequired().HasMaxLength(200);
        builder.Property(p => p.Reference).HasMaxLength(100);
        builder.Property(p => p.GlJournalNo).HasMaxLength(30);
        builder.Property(p => p.CreatedAt).IsRequired();

        builder.HasOne(p => p.Grn).WithMany(g => g.Payments).HasForeignKey(p => p.GrnId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(p => p.Creator).WithMany().HasForeignKey(p => p.CreatedBy).OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(p => p.GrnId);
    }
}

public sealed class GrnLineConfiguration : IEntityTypeConfiguration<GrnLine>
{
    public void Configure(EntityTypeBuilder<GrnLine> builder)
    {
        builder.ToTable("grn_lines");
        builder.HasKey(l => l.GrnLineId);

        builder.Property(l => l.Qty).HasPrecision(18, 4);
        builder.Property(l => l.UnitCost).HasPrecision(18, 6);
        builder.Property(l => l.PoUnitPrice).HasPrecision(18, 6);
        builder.Property(l => l.DiscountPct).HasPrecision(9, 4);
        builder.Property(l => l.NetUnitCost).HasPrecision(18, 6);
        builder.Property(l => l.VatPct).HasPrecision(9, 4);
        builder.Property(l => l.VatAmount).HasPrecision(18, 4);
        builder.Property(l => l.ReceivedValue).HasPrecision(18, 4);
        builder.Property(l => l.LineTotal).HasPrecision(18, 4);
        builder.Property(l => l.HoldStatus).HasConversion<string>().HasMaxLength(20).IsRequired();

        builder.HasOne(l => l.Grn).WithMany(g => g.Lines).HasForeignKey(l => l.GrnId).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(l => l.PoLine).WithMany().HasForeignKey(l => l.PoLineId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.Item).WithMany().HasForeignKey(l => l.ItemId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.Bin).WithMany().HasForeignKey(l => l.BinId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.Batch).WithMany().HasForeignKey(l => l.BatchId).OnDelete(DeleteBehavior.Restrict);
    }
}

public sealed class GrnLineWarrantyNumberConfiguration : IEntityTypeConfiguration<GrnLineWarrantyNumber>
{
    public void Configure(EntityTypeBuilder<GrnLineWarrantyNumber> builder)
    {
        builder.ToTable("grn_line_warranty_numbers");
        builder.HasKey(w => w.GrnLineWarrantyNumberId);

        builder.Property(w => w.WarrantyNo).IsRequired().HasMaxLength(100);

        builder.HasOne(w => w.GrnLine).WithMany(l => l.WarrantyNumbers)
            .HasForeignKey(w => w.GrnLineId).OnDelete(DeleteBehavior.Cascade);

        // A warranty number entered twice on the same line is almost certainly a typo —
        // catch it at the DB, not just client-side.
        builder.HasIndex(w => new { w.GrnLineId, w.WarrantyNo }).IsUnique();
    }
}
