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

namespace Extrudex.Infrastructure.Data.Configurations;

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

        builder.Property(e => e.Name)
            .HasColumnName("name")
            .IsRequired()
            .HasMaxLength(200);

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

        builder.Property(e => e.Manufacturer)
            .HasColumnName("manufacturer")
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(e => e.Model)
            .HasColumnName("model")
            .IsRequired()
            .HasMaxLength(200);

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

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

        builder.Property(e => e.HostnameOrIp)
            .HasColumnName("hostname_or_ip")
            .IsRequired()
            .HasMaxLength(255);

        builder.Property(e => e.Port)
            .HasColumnName("port")
            .IsRequired();

        // MQTT credentials
        builder.Property(e => e.MqttUsername)
            .HasColumnName("mqtt_username")
            .HasMaxLength(200);

        builder.Property(e => e.MqttPassword)
            .HasColumnName("mqtt_password")
            .HasMaxLength(500);

        builder.Property(e => e.MqttUseTls)
            .HasColumnName("mqtt_use_tls")
            .HasDefaultValue(false)
            .IsRequired();

        // Moonraker API key
        builder.Property(e => e.ApiKey)
            .HasColumnName("api_key")
            .HasMaxLength(500);

        builder.Property(e => e.IsActive)
            .HasColumnName("is_active")
            .HasDefaultValue(true)
            .IsRequired();

        builder.Property(e => e.LastSeenAt)
            .HasColumnName("last_seen_at");

        // Index on status for filtering online/offline printers
        builder.HasIndex(e => e.Status)
            .HasDatabaseName("ix_printers_status");

        // Index on printer_type for filtering by printer hardware type
        builder.HasIndex(e => e.PrinterType)
            .HasDatabaseName("ix_printers_printer_type");

        // Index on connection for querying by protocol
        builder.HasIndex(e => e.ConnectionType)
            .HasDatabaseName("ix_printers_connection_type");

        // Index on is_active for active printer queries
        builder.HasIndex(e => e.IsActive)
            .HasDatabaseName("ix_printers_is_active");

        // Relationships
        builder.HasMany(e => e.AmsUnits)
            .WithOne(e => e.Printer)
            .HasForeignKey(e => e.PrinterId)
            .HasConstraintName("fk_ams_units_printer");

        builder.HasMany(e => e.PrintJobs)
            .WithOne(e => e.Printer)
            .HasForeignKey(e => e.PrinterId)
            .HasConstraintName("fk_print_jobs_printer");
    }
}