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

namespace Extrudex.Infrastructure.Data.Configurations;

/// <summary>
/// EF Core configuration for the UsageLog entity.
/// Maps to the usage_logs table with snake_case columns and appropriate indexes.
/// </summary>
public class UsageLogConfiguration : BaseEntityConfiguration<UsageLog>
{
    /// <inheritdoc/>
    public override void Configure(EntityTypeBuilder<UsageLog> builder)
    {
        base.Configure(builder);

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

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

        builder.Property(e => e.PrintJobId)
            .HasColumnName("print_job_id");

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

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

        builder.Property(e => e.UsageTimestamp)
            .HasColumnName("usage_timestamp")
            .IsRequired();

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

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

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

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

        // Index on print_job_id for querying usage by print job
        builder.HasIndex(e => e.PrintJobId)
            .HasDatabaseName("ix_usage_logs_print_job_id");

        // Index on usage_timestamp for chronological queries
        builder.HasIndex(e => e.UsageTimestamp)
            .HasDatabaseName("ix_usage_logs_usage_timestamp");

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

        // Relationships
        builder.HasOne(e => e.Spool)
            .WithMany()
            .HasForeignKey(e => e.SpoolId)
            .HasConstraintName("fk_usage_logs_spool")
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(e => e.Printer)
            .WithMany()
            .HasForeignKey(e => e.PrinterId)
            .HasConstraintName("fk_usage_logs_printer")
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasOne(e => e.PrintJob)
            .WithMany()
            .HasForeignKey(e => e.PrintJobId)
            .HasConstraintName("fk_usage_logs_print_job")
            .OnDelete(DeleteBehavior.SetNull);
    }
}