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

namespace SolutionErp.Infrastructure.Persistence.Configurations;

public class DepartmentConfiguration : IEntityTypeConfiguration<Department>
{
    public void Configure(EntityTypeBuilder<Department> b)
    {
        b.ToTable("Departments");
        b.HasKey(x => x.Id);

        b.Property(x => x.Code).HasMaxLength(50).IsRequired();
        b.Property(x => x.Name).HasMaxLength(200).IsRequired();
        b.Property(x => x.Note).HasMaxLength(1000);

        b.HasIndex(x => x.Code).IsUnique().HasFilter("[IsDeleted] = 0");   // Mig 47 (gotcha #57 EXT) — soft-deleted slot reusable, khớp HasQueryFilter !IsDeleted app-check
        b.HasIndex(x => x.ParentId);   // cây tổ chức — loose-Guid KHÔNG HasOne self-FK (convention loose-Guid hệ này)

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