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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class EmployeeDocumentConfiguration : IEntityTypeConfiguration<EmployeeDocument>
{
    public void Configure(EntityTypeBuilder<EmployeeDocument> builder)
    {
        builder.ToTable("hr_employee_documents");
        builder.HasKey(d => d.EmployeeDocumentId);

        builder.Property(d => d.OriginalFileName).IsRequired().HasMaxLength(260);
        builder.Property(d => d.StoredFileName).IsRequired().HasMaxLength(260);
        builder.Property(d => d.RelativePath).IsRequired().HasMaxLength(500);
        builder.Property(d => d.ContentType).IsRequired().HasMaxLength(200);
        builder.Property(d => d.Notes).HasMaxLength(1000);

        builder.Property(d => d.Status)
            .HasConversion<string>().HasMaxLength(20).IsRequired()
            .HasDefaultValue(EmployeeDocumentStatus.Active);

        builder.HasOne(d => d.Employee).WithMany()
            .HasForeignKey(d => d.EmployeeId).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(d => d.HrDocumentType).WithMany()
            .HasForeignKey(d => d.HrDocumentTypeId).OnDelete(DeleteBehavior.Restrict);

        builder.Property(d => d.UploadedAt).IsRequired();
        builder.Property(d => d.RowVersion).IsRowVersion();

        builder.HasIndex(d => d.EmployeeId);
        builder.HasIndex(d => d.ExpiryDate);
    }
}
