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

namespace OmsorgCore.Infrastructure.Persistence.Configurations;

public class TimeEntryConfiguration : IEntityTypeConfiguration<TimeEntry>
{
    public void Configure(EntityTypeBuilder<TimeEntry> builder)
    {
        builder.ToTable("time_entries");
        builder.HasKey(t => t.Id);
        builder.Property(t => t.NightHours).HasColumnType("numeric(6,2)");
        builder.Property(t => t.SaturdayHours).HasColumnType("numeric(6,2)");
        builder.Property(t => t.SundayHours).HasColumnType("numeric(6,2)");
        builder.Property(t => t.HolidayHours).HasColumnType("numeric(6,2)");
        builder.Property(t => t.AdminNote).HasMaxLength(500);

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

        builder.HasOne(t => t.Order)
            .WithMany()
            .HasForeignKey(t => t.OrderId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(t => t.Status)
            .WithMany()
            .HasForeignKey(t => t.StatusId)
            .OnDelete(DeleteBehavior.Restrict);

        // Soft-gelöschte Zeiterfassungen sind für alle normalen Queries unsichtbar.
        builder.HasQueryFilter(t => !t.IsDeleted);
    }
}
