using adas_core.Application.Exceptions;
using adas_core.Application.Services.Interfaces;
using adas_core.Authentication.Attributes;
using adas_core.Domain.Enums;
using adas_core.Domain.Models.GroupedObservations;
using adas_core.Logging;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;

namespace adas_core.Controllers;

[Route("grouped-observations")]
[ApiController]
public class GroupedObservationsController : ControllerBase
{
    private readonly IGroupedObservationService _groupedObservationService;

    public GroupedObservationsController(IGroupedObservationService groupedObservationService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _groupedObservationService = groupedObservationService;
    }


    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetGroupedObservationAsync(string id, GroupedField groupedConfig)
    {
        if (string.IsNullOrEmpty(id) || !ObjectId.TryParse(id, out var objectId))
            throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        var groupedObs = await _groupedObservationService.GenerateGroupedObservation(objectId, groupedConfig);
        return Ok(groupedObs);
    }
}