using Monitoring.Core.Domain.EventManagement;
using Monitoring.Core.Domain.UserPrimitives;

namespace Monitoring.Core.Domain.CameraManagement;

/// <summary>
/// Просмотр камеры.
/// </summary>
public class CameraView
{
	/// <summary>
	/// Идентификатор.
	/// </summary>
	public CameraViewId Id { get; } = null!;

	/// <summary>
	/// Пользователь, кто просмотрел.
	/// </summary>
	public User User { get; } = null!;

	/// <summary>
	/// ИД пользователя, кто просмотрел.
	/// </summary>
	public UserId UserId { get; } = null!;

	/// <summary>
	/// Камера.
	/// </summary>
	public Camera Camera { get; } = null!;

	/// <summary>
	/// ИД камеры.
	/// </summary>
	public CameraId CameraId { get; } = null!;

	/// <summary>
	/// Тревога.
	/// </summary>
	public Alarm Alarm { get; } = null!;

	/// <summary>
	/// ИД тревоги.
	/// </summary>
	public AlarmId AlarmId { get; } = null!;

	/// <summary>
	/// Дата и время просмотра.
	/// </summary>
	public DateTime DateTime { get; private set; } = DateTime.UtcNow;

	/// <summary>
	/// Дата и время начала видео.	
	/// </summary>
	public DateTime StartVideo { get; private set; } = DateTime.UtcNow;

	/// <summary>
	/// Дата и время окончания видео.
	/// </summary>
	public DateTime EndVideo { get; private set; } = DateTime.UtcNow;

	/// <summary>
	/// Создает экземпляр просмотра.
	/// </summary>
	/// <param name="userId">ИД пользователя.</param>
	/// <param name="cameraId">ИД камеры.</param>
	/// <param name="alarmId">ИД тревоги.</param>
	/// <param name="dateTime">Дата и время просмотра.</param>
	/// <param name="startVideo">Дата и время начала видео.</param>
	/// <param name="endVideo">Дата и время окончания видео.</param>
	/// <returns>Экземпляр просмотра камеры.</returns>
	public static CameraView Create(
		UserId userId,
		CameraId cameraId,
		AlarmId alarmId,
		DateTime dateTime,
		DateTime startVideo,
		DateTime endVideo
	)
	{
		if (startVideo > endVideo)
		{
			throw new ArgumentException("StartVideo must be less than or equal to EndVideo");
		}
		return new(
			new(Guid.CreateVersion7()),
			userId,
			cameraId,
			alarmId,
			dateTime,
			startVideo,
			endVideo
		);
	}

	/// <summary>
	/// Обновить времена просмотра.
	/// </summary>
	/// <param name="dateTime">Дата и время просмотра.</param>
	/// <param name="startVideo">Начало видео.</param>
	/// <param name="endVideo">Окончание видео.</param>
	public void UpdateTimes(DateTime dateTime, DateTime startVideo, DateTime endVideo)
	{
		if (startVideo > endVideo)
		{
			throw new ArgumentException("StartVideo must be less than or equal to EndVideo");
		}

		this.DateTime = dateTime;
		this.StartVideo = startVideo;
		this.EndVideo = endVideo;
	}

	private CameraView(
		CameraViewId id,
		UserId userId,
		CameraId cameraId,
		AlarmId alarmId,
		DateTime dateTime,
		DateTime startVideo,
		DateTime endVideo
	)
	{
		this.Id = id;
		this.UserId = userId;
		this.CameraId = cameraId;
		this.AlarmId = alarmId;
		this.DateTime = dateTime;
		this.StartVideo = startVideo;
		this.EndVideo = endVideo;
	}

	/// <summary>
	/// Constructor for EF.
	/// </summary>
	protected CameraView() { }
}
