using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Monitoring.Common;
using Monitoring.Controllers;
using Monitoring.Core.Domain.EventManagement;
using Monitoring.Extensions;
using Monitoring.Features.EventCodes.GetEventList;
using Monitoring.Infrastructure.Data;
using Monitoring.Models;

namespace Monitoring.Features.EventCodes;
[Route("api/event-codes")]
[Authorize(Roles = Roles.Manager)]
public class EventCodesController(
	DataContext dataContext,
	ILogger<EventCodesController> logger
) : BaseController
{
	public sealed record EventCodeResponse(
		Guid Id,
		int Value,
		EventCodeGroup Group,
		bool IsActive
	);

	[HttpGet(Name = "GetEventCodeList")]
	[ProducesResponseType<TableDataResponseModel<EventCodeResponse>>(StatusCodes.Status200OK)]
	public async Task<IActionResult> GetList(
		[FromQuery] GetEventCodeListRequest request,
		CancellationToken cancellationToken
	)
	{
		logger.LogInformation("Try to get event codes list");

		var events = await dataContext.EventCodes
			.AsNoTracking()
			.OrderBy(x => x.Value)
			.GetTable(request, x => new EventCodeResponse(
				x.Id.Value,
				x.Value.Value,
				x.Group,
				x.IsActive
			), cancellationToken);

		return Ok(events);
	}
}
