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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class AttendanceRecordConfiguration : IEntityTypeConfiguration<AttendanceRecord>
{
    public void Configure(EntityTypeBuilder<AttendanceRecord> builder)
    {
        builder.ToTable("hr_attendance_records");
        builder.HasKey(r => r.AttendanceRecordId);

        builder.Property(r => r.Notes).HasMaxLength(1000);
        builder.Property(r => r.AttendanceStatus).HasConversion<string>().HasMaxLength(20).IsRequired();
        builder.Property(r => r.RowValidationStatus).HasConversion<string>().HasMaxLength(20).IsRequired();

        builder.HasOne(r => r.AttendanceUploadBatch).WithMany()
            .HasForeignKey(r => r.AttendanceUploadBatchId).OnDelete(DeleteBehavior.Cascade);
        builder.HasOne(r => r.Employee).WithMany()
            .HasForeignKey(r => r.EmployeeId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(r => r.WorkShift).WithMany()
            .HasForeignKey(r => r.WorkShiftId).OnDelete(DeleteBehavior.Restrict);

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

        builder.HasIndex(r => new { r.EmployeeId, r.AttendanceDate });
        builder.HasIndex(r => r.AttendanceUploadBatchId);
    }
}
