using System.Text.Json.Serialization;

namespace Flyshot.Core.Domain;

/// <summary>
/// Represents an alarm surfaced by planning, gateway, or runtime subsystems.
/// </summary>
public sealed class RuntimeAlarm
{
    /// <summary>
    /// Initializes a validated runtime alarm.
    /// </summary>
    public RuntimeAlarm(string code, string message, AlarmSeverity severity, DateTimeOffset raisedAt, bool isAcknowledged = false)
    {
        if (string.IsNullOrWhiteSpace(code))
        {
            throw new ArgumentException("Alarm code is required.", nameof(code));
        }

        if (string.IsNullOrWhiteSpace(message))
        {
            throw new ArgumentException("Alarm message is required.", nameof(message));
        }

        Code = code;
        Message = message;
        Severity = severity;
        RaisedAt = raisedAt;
        IsAcknowledged = isAcknowledged;
    }

    /// <summary>
    /// Gets the stable alarm code.
    /// </summary>
    [JsonPropertyName("code")]
    public string Code { get; }

    /// <summary>
    /// Gets the human-readable alarm message.
    /// </summary>
    [JsonPropertyName("message")]
    public string Message { get; }

    /// <summary>
    /// Gets the alarm severity level.
    /// </summary>
    [JsonPropertyName("severity")]
    public AlarmSeverity Severity { get; }

    /// <summary>
    /// Gets the time the alarm was raised.
    /// </summary>
    [JsonPropertyName("raisedAt")]
    public DateTimeOffset RaisedAt { get; }

    /// <summary>
    /// Gets a value indicating whether the alarm has been acknowledged.
    /// </summary>
    [JsonPropertyName("isAcknowledged")]
    public bool IsAcknowledged { get; }
}

/// <summary>
/// Identifies the severity level of a runtime alarm.
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum AlarmSeverity
{
    /// <summary>
    /// Informational alarm or notice.
    /// </summary>
    Info,

    /// <summary>
    /// Warning that does not yet block execution.
    /// </summary>
    Warning,

    /// <summary>
    /// Error that blocks the current operation.
    /// </summary>
    Error,

    /// <summary>
    /// Critical fault that requires immediate operator attention.
    /// </summary>
    Critical
}
