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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class LeaveRequestConfiguration : IEntityTypeConfiguration<LeaveRequest>
{
    public void Configure(EntityTypeBuilder<LeaveRequest> builder)
    {
        builder.ToTable("hr_leave_requests");
        builder.HasKey(r => r.LeaveRequestId);

        builder.Property(r => r.DocNo).IsRequired().HasMaxLength(30);
        builder.HasIndex(r => r.DocNo).IsUnique();
        builder.Property(r => r.DaysCount).HasPrecision(6, 2);
        builder.Property(r => r.Reason).HasMaxLength(1000);
        builder.Property(r => r.RejectionReason).HasMaxLength(1000);

        builder.Property(r => r.Status).HasConversion<string>().HasMaxLength(20).IsRequired();

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

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

        builder.HasIndex(r => r.EmployeeId);
        builder.HasIndex(r => r.Status);
        builder.HasIndex(r => new { r.StartDate, r.EndDate });
    }
}
