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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class LeaveBalanceConfiguration : IEntityTypeConfiguration<LeaveBalance>
{
    public void Configure(EntityTypeBuilder<LeaveBalance> builder)
    {
        builder.ToTable("hr_leave_balances");
        builder.HasKey(b => b.LeaveBalanceId);

        builder.Property(b => b.EntitledDays).HasPrecision(6, 2);
        builder.Property(b => b.TakenDays).HasPrecision(6, 2);
        builder.Property(b => b.CarriedForwardDays).HasPrecision(6, 2);
        builder.Property(b => b.AdjustmentDays).HasPrecision(6, 2);

        builder.HasOne(b => b.Employee).WithMany()
            .HasForeignKey(b => b.EmployeeId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(b => b.LeaveType).WithMany()
            .HasForeignKey(b => b.LeaveTypeId).OnDelete(DeleteBehavior.Restrict);

        builder.Property(b => b.RowVersion).IsRowVersion();

        builder.HasIndex(b => new { b.EmployeeId, b.LeaveTypeId, b.Year }).IsUnique();
    }
}
