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

namespace adas_core.Controllers;

[Route("device")]
[ApiController]
public class DeviceController : ControllerBase
{
    private readonly IDeviceService _deviceService;

    public DeviceController(IDeviceService deviceService)
    {
        _deviceService = deviceService;
    }
    
    [HttpPost("add")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> CreateDevice([FromBody] DeviceDto device)
    {
        var result = await _deviceService.Create(device);
        if(result is null) Conflict("Unable to create device");
        return Ok(result);
    }
    
    [HttpDelete("delete/{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> CreateDevice(string id)
    {
        if(!ObjectId.TryParse(id, out ObjectId deviceId)) return BadRequest("Invalid device Id");
        var result = await _deviceService.Delete(deviceId);
        if(result is false) Conflict("Unable to delete device");
        return Ok(result);
    }
    
    [HttpPut("edit")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> EditDevice([FromBody] DeviceDto device)
    {
        var result = await _deviceService.Update(device);
        if(result is null) Conflict("Unable to update device");
        return Ok(result);
    } 
    
    [HttpPost("event")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    public async Task<IActionResult> ReceiveEventDevice([FromBody] DeviceDto device)
    {
        var result = await _deviceService.ReceiveEvent(device);
        if(result is null) Conflict("Unable to create device");
        return Ok();
    }
}