using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.PurchaseEvaluations;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// [S61 Mig 50] Ngân sách gói thầu per cặp (ProjectId, WorkItemId) — thay module
// Budget cũ. Loose-Guid convention PE: KHÔNG FK vật lý, KHÔNG navigation.
// UNIQUE composite FILTERED [IsDeleted] = 0 day-1 (gotcha #57 — soft-delete +
// UNIQUE phải filter, filter string byte-for-byte khớp 13 index filtered hiện có).
public class PeWorkItemBudgetConfiguration : IEntityTypeConfiguration<PeWorkItemBudget>
{
    public void Configure(EntityTypeBuilder<PeWorkItemBudget> b)
    {
        b.ToTable("PeWorkItemBudgets");
        b.HasKey(x => x.Id);

        // Precision match BudgetManualAmount cũ (18,2).
        b.Property(x => x.ProEstimateAmount).HasPrecision(18, 2);
        b.Property(x => x.ProInitialAmount).HasPrecision(18, 2);
        b.Property(x => x.ProAdjustmentAmount).HasPrecision(18, 2);
        b.Property(x => x.InitialAmount).HasPrecision(18, 2);
        b.Property(x => x.AdjustmentAmount).HasPrecision(18, 2);
        b.Property(x => x.ProNote).HasMaxLength(1000);
        b.Property(x => x.CcmNote).HasMaxLength(1000);

        b.HasIndex(x => new { x.ProjectId, x.WorkItemId })
            .IsUnique()
            .HasFilter("[IsDeleted] = 0");
        b.HasIndex(x => x.WorkItemId);

        b.HasQueryFilter(x => !x.IsDeleted);
    }
}
