namespace PowderCoating.Core.Entities;

/// <summary>
/// Platform-level community library entry for a shared custom formula template.
/// Not tenant-scoped — no BaseEntity, no CompanyId, no soft delete.
/// Shared voluntarily by the originating company; imported as independent copies by others.
/// </summary>
public class FormulaLibraryItem
{
    public int Id { get; set; }

    // ── Formula content (copied from CustomItemTemplate at share time) ─────

    public string Name { get; set; } = string.Empty;
    public string? Description { get; set; }

    /// <summary>"FixedRate" or "SurfaceAreaSqFt" — mirrors CustomItemTemplate.OutputMode.</summary>
    public string OutputMode { get; set; } = "FixedRate";

    /// <summary>JSON array of field definitions: [{name, label, unit, defaultValue}]</summary>
    public string FieldsJson { get; set; } = "[]";

    /// <summary>NCalc expression using field name slugs and the reserved variable 'rate'.</summary>
    public string Formula { get; set; } = string.Empty;

    public decimal? DefaultRate { get; set; }
    public string? RateLabel { get; set; }
    public string? Notes { get; set; }

    /// <summary>
    /// Blob path referencing the source template's diagram image.
    /// Nulled out (here and on all imports) if the source template's diagram is removed.
    /// </summary>
    public string? DiagramImagePath { get; set; }

    // ── Attribution ────────────────────────────────────────────────────────

    /// <summary>Comma-separated community tags, e.g. "HVAC,Sheet Metal".</summary>
    public string? Tags { get; set; }

    /// <summary>Optional industry hint shown on the browse card, e.g. "HVAC", "Automotive".</summary>
    public string? IndustryHint { get; set; }

    /// <summary>Id of the CustomItemTemplate this was shared from.</summary>
    public int SourceCustomItemTemplateId { get; set; }

    public int SourceCompanyId { get; set; }

    /// <summary>Denormalized company name so it renders without a join when the company is gone.</summary>
    public string SourceCompanyName { get; set; } = string.Empty;

    /// <summary>
    /// When non-null, this entry was derived from an imported formula that was subsequently
    /// modified. Points to the original library entry. Shown as "Inspired by..." on the browse card.
    /// </summary>
    public int? InspiredByFormulaLibraryItemId { get; set; }
    public virtual FormulaLibraryItem? InspiredBy { get; set; }

    public string SharedByUserId { get; set; } = string.Empty;
    public DateTime SharedAt { get; set; } = DateTime.UtcNow;

    /// <summary>False when the creator has removed it from the community library.</summary>
    public bool IsPublished { get; set; } = true;

    /// <summary>Running count of how many companies have imported this entry.</summary>
    public int ImportCount { get; set; }

    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime? UpdatedAt { get; set; }
}
