using CMSMicroservice.Domain.Entities.Commission;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

public class WorkerExecutionLogConfiguration : IEntityTypeConfiguration<WorkerExecutionLog>
{
    public void Configure(EntityTypeBuilder<WorkerExecutionLog> builder)
    {
        builder.ToTable("WorkerExecutionLogs", "CMS");

        builder.HasKey(x => x.Id);

        builder.Property(x => x.ExecutionId)
            .IsRequired();

        builder.Property(x => x.WeekDefinitionId)
            .IsRequired();

        builder.Property(x => x.StartedAt)
            .IsRequired();

        builder.Property(x => x.Status)
            .IsRequired();

        builder.Property(x => x.ErrorMessage)
            .HasMaxLength(2000);

        builder.Property(x => x.Details)
            .HasColumnType("nvarchar(max)");

        // رابطه با WeekDefinition (Required FK)
        builder.HasOne(x => x.WeekDefinition)
            .WithMany(wd => wd.WorkerExecutionLogs)
            .HasForeignKey(x => x.WeekDefinitionId)
            .IsRequired()
            .OnDelete(DeleteBehavior.Restrict);

        // Index for querying by WeekDefinitionId
        builder.HasIndex(x => x.WeekDefinitionId);

        // Index for querying by execution time
        builder.HasIndex(x => x.StartedAt);

        // Index for querying by status
        builder.HasIndex(x => x.Status);
    }
}
