namespace ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;

public class TemplateScript
{
    /// <summary>
    /// The script identifier.
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// The template identifier this script belongs to.
    /// </summary>
    public int TemplateId { get; set; }

    /// <summary>
    /// The script name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Whether the script is locked for editing.
    /// </summary>
    public bool IsLocked { get; set; }

    /// <summary>
    /// The script code.
    /// </summary>
    public string Code { get; set; }

    /// <summary>
    /// The trigger type for the script, or null.
    /// </summary>
    public string? TriggerType { get; set; }

    /// <summary>
    /// The trigger configuration, or null.
    /// </summary>
    public string? TriggerConfiguration { get; set; }

    /// <summary>
    /// The parameter definitions in JSON format, or null.
    /// </summary>
    public string? ParameterDefinitions { get; set; }

    /// <summary>
    /// The return type definition in JSON format, or null.
    /// </summary>
    public string? ReturnDefinition { get; set; }

    /// <summary>
    /// The minimum time between script runs, or null.
    /// </summary>
    public TimeSpan? MinTimeBetweenRuns { get; set; }

    /// <summary>
    /// Per-script execution timeout in seconds, or null to use the site's global
    /// default (<c>SiteRuntimeOptions.ScriptExecutionTimeoutSeconds</c>). A
    /// non-positive value (≤ 0) is treated the same as null — i.e. fall back to
    /// the global default — by the Site Runtime. Seconds (not a TimeSpan) to keep
    /// the unit consistent with the global option it overrides.
    /// </summary>
    public int? ExecutionTimeoutSeconds { get; set; }

    /// <summary>
    /// True when this row was copied from the base template and has not been
    /// overridden on the derived template. Changes to the base flow downward
    /// for inherited rows; an explicit override flips this to false.
    /// Always false on base (non-derived) templates.
    /// </summary>
    public bool IsInherited { get; set; }

    /// <summary>
    /// Set on a base script. When true, derived templates may not override
    /// the script body — the row is rendered readonly in the derived
    /// UI, and any attempt to update it through the API is rejected.
    /// </summary>
    public bool LockedInDerived { get; set; }

    /// <summary>
    /// Initializes a new instance of the TemplateScript.
    /// </summary>
    /// <param name="name">The script name.</param>
    /// <param name="code">The script code.</param>
    public TemplateScript(string name, string code)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
        Code = code ?? throw new ArgumentNullException(nameof(code));
    }
}
