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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class EmployeeSalaryStructureConfiguration : IEntityTypeConfiguration<EmployeeSalaryStructure>
{
    public void Configure(EntityTypeBuilder<EmployeeSalaryStructure> builder)
    {
        builder.ToTable("hr_employee_salary_structures");
        builder.HasKey(s => s.EmployeeSalaryStructureId);

        builder.Property(s => s.BasicSalary).HasPrecision(18, 2);
        builder.Property(s => s.Currency).IsRequired().HasMaxLength(3);
        builder.Property(s => s.Status).HasConversion<string>().HasMaxLength(20).IsRequired();

        builder.HasOne(s => s.Employee).WithMany()
            .HasForeignKey(s => s.EmployeeId).OnDelete(DeleteBehavior.Restrict);

        builder.HasMany(s => s.Lines).WithOne(l => l.EmployeeSalaryStructure!)
            .HasForeignKey(l => l.EmployeeSalaryStructureId).OnDelete(DeleteBehavior.Cascade);

        builder.Property(s => s.CreatedAt).IsRequired();
        builder.Property(s => s.RowVersion).IsRowVersion();

        builder.HasIndex(s => new { s.EmployeeId, s.EffectiveTo });
    }
}

public sealed class EmployeeSalaryStructureLineConfiguration : IEntityTypeConfiguration<EmployeeSalaryStructureLine>
{
    public void Configure(EntityTypeBuilder<EmployeeSalaryStructureLine> builder)
    {
        builder.ToTable("hr_employee_salary_structure_lines");
        builder.HasKey(l => l.EmployeeSalaryStructureLineId);

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

        builder.HasOne(l => l.SalaryComponent).WithMany()
            .HasForeignKey(l => l.SalaryComponentId).OnDelete(DeleteBehavior.Restrict);
    }
}
