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

namespace Extrudex.Infrastructure.Data.Configurations;

public class PrintJobConfiguration : BaseEntityConfiguration<PrintJob>
{
    public override void Configure(EntityTypeBuilder<PrintJob> builder)
    {
        base.Configure(builder);

        builder.Property(e => e.PrinterId)
            .HasColumnName("printer_id")
            .IsRequired();

        builder.Property(e => e.SpoolId)
            .HasColumnName("spool_id")
            .IsRequired();

        builder.Property(e => e.PrintName)
            .HasColumnName("print_name")
            .IsRequired()
            .HasMaxLength(500);

        builder.Property(e => e.GcodeFilePath)
            .HasColumnName("gcode_file_path")
            .HasMaxLength(1000);

        builder.Property(e => e.MmExtruded)
            .HasColumnName("mm_extruded")
            .HasPrecision(12, 2)
            .IsRequired();

        builder.Property(e => e.GramsDerived)
            .HasColumnName("grams_derived")
            .HasPrecision(10, 2)
            .IsRequired();

        builder.Property(e => e.CostPerPrint)
            .HasColumnName("cost_per_print")
            .HasPrecision(10, 4);

        builder.Property(e => e.StartedAt)
            .HasColumnName("started_at");

        builder.Property(e => e.CompletedAt)
            .HasColumnName("completed_at");

        builder.Property(e => e.Status)
            .HasColumnName("status")
            .HasConversion<string>()
            .HasMaxLength(50)
            .HasDefaultValue(JobStatus.Queued)
            .IsRequired();

        builder.Property(e => e.DataSource)
            .HasColumnName("data_source")
            .HasConversion<string>()
            .HasMaxLength(50)
            .IsRequired();

        // Audit snapshots for COGS accuracy
        builder.Property(e => e.FilamentDiameterAtPrintMm)
            .HasColumnName("filament_diameter_at_print_mm")
            .HasPrecision(6, 3)
            .IsRequired();

        builder.Property(e => e.MaterialDensityAtPrint)
            .HasColumnName("material_density_at_print")
            .HasPrecision(10, 4)
            .IsRequired();

        builder.Property(e => e.Notes)
            .HasColumnName("notes")
            .HasMaxLength(2000);

        // Index on status for filtering active/completed jobs
        builder.HasIndex(e => e.Status)
            .HasDatabaseName("ix_print_jobs_status");

        // Index on printer_id for querying jobs by printer
        builder.HasIndex(e => e.PrinterId)
            .HasDatabaseName("ix_print_jobs_printer_id");

        // Index on spool_id for querying jobs by spool
        builder.HasIndex(e => e.SpoolId)
            .HasDatabaseName("ix_print_jobs_spool_id");

        // Index on data_source for querying by integration path
        builder.HasIndex(e => e.DataSource)
            .HasDatabaseName("ix_print_jobs_data_source");

        // Relationships
        builder.HasOne(e => e.Printer)
            .WithMany(e => e.PrintJobs)
            .HasForeignKey(e => e.PrinterId)
            .HasConstraintName("fk_print_jobs_printer")
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(e => e.Spool)
            .WithMany(e => e.PrintJobs)
            .HasForeignKey(e => e.SpoolId)
            .HasConstraintName("fk_print_jobs_spool")
            .OnDelete(DeleteBehavior.Restrict);
    }
}