using Extrudex.Domain.Base;
using Extrudex.Domain.Enums;

namespace Extrudex.Domain.Entities;

/// <summary>
/// Represents a single print job. Tracks which printer and spool were used,
/// how much filament was consumed, and audit snapshots of material properties
/// at the time of printing (to preserve COGS accuracy even if material data
/// changes later).
/// </summary>
public class PrintJob : AuditableEntity
{
    /// <summary>
    /// Foreign key to the printer that executed this print job.
    /// </summary>
    public Guid PrinterId { get; set; }

    /// <summary>
    /// Navigation to the printer that executed this print job.
    /// </summary>
    public Printer Printer { get; set; } = null!;

    /// <summary>
    /// Foreign key to the spool that provided filament for this print job.
    /// </summary>
    public Guid SpoolId { get; set; }

    /// <summary>
    /// Navigation to the spool that provided filament for this print job.
    /// </summary>
    public Spool Spool { get; set; } = null!;

    /// <summary>
    /// Human-readable name or identifier for the print job.
    /// </summary>
    public string PrintName { get; set; } = string.Empty;

    /// <summary>
    /// Path or filename of the G-code file being printed.
    /// </summary>
    public string? GcodeFilePath { get; set; }

    /// <summary>
    /// Total millimeters of filament extruded during this print job.
    /// The primary input to the COGS derivation formula.
    /// </summary>
    public decimal MmExtruded { get; set; }

    /// <summary>
    /// Derived grams consumed for this print, calculated as:
    /// mm_extruded × cross_section_area × material_density_at_print.
    /// </summary>
    public decimal GramsDerived { get; set; }

    /// <summary>
    /// Calculated cost of goods sold (COGS) for this print job.
    /// Derived from grams consumed and the spool's purchase price.
    /// </summary>
    public decimal? CostPerPrint { get; set; }

    /// <summary>
    /// Timestamp when the print job started (UTC).
    /// </summary>
    public DateTime? StartedAt { get; set; }

    /// <summary>
    /// Timestamp when the print job completed or failed (UTC).
    /// </summary>
    public DateTime? CompletedAt { get; set; }

    /// <summary>
    /// Current status of the print job.
    /// </summary>
    public JobStatus Status { get; set; } = JobStatus.Queued;

    /// <summary>
    /// The source of the print job data (which integration path provided it).
    /// </summary>
    public DataSource DataSource { get; set; }

    /// <summary>
    /// Audit snapshot: the filament diameter (mm) recorded at the time of printing.
    /// Preserved so COGS calculations remain accurate even if the spool's
    /// diameter is later corrected.
    /// </summary>
    public decimal FilamentDiameterAtPrintMm { get; set; }

    /// <summary>
    /// Audit snapshot: the material density (g/cm³) recorded at the time of printing.
    /// Preserved so COGS calculations remain accurate even if the material's
    /// density is later corrected.
    /// </summary>
    public decimal MaterialDensityAtPrint { get; set; }

    /// <summary>
    /// Optional notes about the print job (e.g., "First layer adhesion issues").
    /// </summary>
    public string? Notes { get; set; }

    /// <summary>
    /// Navigation collection of filament usage records for this print job.
    /// Enables tracking granular per-spool consumption within a print.
    /// </summary>
    public ICollection<FilamentUsage> FilamentUsages { get; set; } = new List<FilamentUsage>();
}