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.Filter;
using adas_core.Domain.Models.MongoModels;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;

namespace adas_core.Controllers;

[Route("camera")]
[ApiController]
public class CameraController(ICameraService cameraService) : ControllerBase
{
    [HttpPost("paginated")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> GetPaginatedCamera([FromBody] PaginationFilter request, bool withPoCs)
    {
        if (request == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var cameras = await cameraService.GetPaginatedCameras(request);
        return Ok(cameras);
    }
    [HttpGet("{textToSearch}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> GetSearchByName(string textToSearch)
    {
        var cameras = await cameraService.GetSearchByNameCameras(textToSearch);
        return Ok(cameras);
    }
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> CreateCamera([FromBody] Camera camera)
    {
        var cam = await cameraService.InsertCamera(camera);
        return Ok(cam);
    }
    [HttpPut("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> UpdateCamera(string id, [FromBody] Camera camera)
    {
        if (!ObjectId.TryParse(id, out var idParsed)) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var cameraUpdated = await cameraService.UpdateCameraById(idParsed, camera);
        return Ok(cameraUpdated);
    }
    [HttpDelete("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> DeleteCamera(string id)
    {
        if (!ObjectId.TryParse(id, out var idParsed)) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var cameraUpdated = await cameraService.DeleteCamera(idParsed);
        return Ok(cameraUpdated);
    }
}