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

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF Mig 42 P11-B Wave 1 (Phase 11) — Số dư phép theo năm.
// FK LeaveType WithMany() Restrict (catalog không cascade). UNIQUE composite
// (UserId, LeaveTypeId, Year). HRM no global HasQueryFilter — list query
// MUST .Where(!IsDeleted) thủ công ở handler.
public class LeaveBalanceConfiguration : IEntityTypeConfiguration<LeaveBalance>
{
    public void Configure(EntityTypeBuilder<LeaveBalance> e)
    {
        e.ToTable("LeaveBalances");

        e.Property(x => x.EntitledDays).HasPrecision(5, 2);
        e.Property(x => x.UsedDays).HasPrecision(5, 2);
        e.Property(x => x.AdjustmentDays).HasPrecision(5, 2);

        e.HasOne(x => x.LeaveType)
            .WithMany()
            .HasForeignKey(x => x.LeaveTypeId)
            .OnDelete(DeleteBehavior.Restrict);

        e.HasIndex(x => new { x.UserId, x.LeaveTypeId, x.Year }).IsUnique();
        e.HasIndex(x => x.UserId);
    }
}
