using Monitoring.Core.Domain.EventManagement;
using Monitoring.Core.Domain.UserLogManagement;

namespace Monitoring.Features.Reports.Contracts;

/// <summary>
/// Логи за сутки.
/// </summary>
/// <param name="Date">Дата.</param>
/// <param name="Logs">Логи.</param>
public record DayLogModel(
	DateOnly Date,
	IEnumerable<LogModel> Logs
)
{
	public static DayLogModel FromEvent(IGrouping<DateTime, Event> dayLogs)
	{
		return new DayLogModel(
			DateOnly.FromDateTime(dayLogs.Key),
			dayLogs.Select(LogModel.FromEvent)
		);
	}

	public static DayLogModel FromUserLog(IGrouping<DateTime, UserLog> dayLogs)
	{
		return new DayLogModel(
			DateOnly.FromDateTime(dayLogs.Key),
			dayLogs.Select(LogModel.FromUserLog)
		);
	}

	public static DayLogModel FromCombined(IGrouping<DateTime, LogGroupModel.Combined> dayLogs)
	{
		return new DayLogModel(
			DateOnly.FromDateTime(dayLogs.Key),
			dayLogs.Select(combined => combined.Event != null
				? LogModel.FromEvent(combined.Event)
				: combined.UserLog is not null
					? LogModel.FromUserLog(combined.UserLog)
					: throw new ArgumentException("Event or UserLog must be set")
			)
		);
	}
};