using System.ComponentModel;
using System.Text.Json.Serialization;
using Monitoring.Core.Domain.AssetManagement;
using Monitoring.Core.Domain.CallManagement;
using Monitoring.Core.Domain.CheckListManagement;

namespace Monitoring.Core.Domain.EventManagement;

/// <summary>
/// Группа одновременно отрабатываемых тревог.
/// </summary>
public class AlarmScope
{
	/// <summary>
	/// ИД группы.
	/// </summary>
	public AlarmScopeId Id { get; } = default!;

	/// <summary>
	/// Объект.
	/// </summary>
	public Asset? Asset { get; }

	/// <summary>
	/// ИД объекта.
	/// </summary>
	public AssetId AssetId { get; } = default!;

	/// <summary>
	/// История изменений статуса тревоги.
	/// </summary>
	public List<AlarmScopeStatusChanging> StatusHistory { get; set; } = [];

	/// <summary>
	/// Список тревог.
	/// </summary>
	public List<Alarm> Alarms { get; } = [];

	/// <summary>
	/// Список проверок.
	/// </summary>
	public List<CheckList> CheckLists { get; } = [];

	/// <summary>
	/// Звноки в данном скопе тревог.
	/// </summary>
	public List<Call> Calls { get; } = [];

	public AlarmScopeResult Result { get; private set; } = AlarmScopeResult.Unknown;

	public AlarmScope(AssetId assetId)
	{
		Id = new AlarmScopeId(Guid.CreateVersion7());
		AssetId = assetId;
		StatusHistory.Add(AlarmScopeStatusChanging.Create(this.Id, null, AlarmScopeStatus.New));
	}

	protected AlarmScope() { }

	public void SetResult(AlarmScopeResult result)
	{
		if (result == AlarmScopeResult.Unknown)
		{
			throw new ArgumentOutOfRangeException("result cannot be unknown");
		}

		if (this.Result != AlarmScopeResult.Unknown)
		{
			throw new ArgumentException(
				"Cannot to set result to Alarm scope. It is already resolved."
			);
		}

		Result = result;
	}
}

public record AlarmScopeId(Guid Value);

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum AlarmScopeResult
{
	Unknown = 0,
	False = 1,
	Real = 2,
}
