using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.Mediator.Rooms.Commands;
using Diligent.WebAPI.Host.Mediator.Rooms.Queries;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Diligent.WebAPI.Host.Controllers
{
    [ApiVersion("1.0")]
    [ApiController]
    [Route("v{version:apiVersion}/[controller]")]
    public class RoomController : ControllerBase
    {
        private readonly IMediator _mediator;

        public RoomController(IMediator mediator)
        {
            _mediator = mediator;
        }

        [Authorize(Roles = "Customer,Support")]
        [HttpGet("rooms-with-filtered-messages")]
        public async Task<ActionResult<List<Room>>> GetAllRoomsWithFilteredMessages(string customerId) =>
            await _mediator.Send(new GetAllRoomsWithFilteredMessagesQuery(customerId));

        [HttpGet("support-rooms")]
        [Authorize(Roles = "Support")]
        public async Task<ActionResult<List<Room>>> GetSupportRooms(string supportId) =>
            await _mediator.Send(new GetRoomsWhichSupportCreatedQuery(supportId));

        [HttpPost]
        [Authorize(Roles = "Support")]
        public async Task<ActionResult<Room>> CreateChat(Room room)
        {
            await _mediator.Send(new CreateRoomCommand(room));
            return room;
        }
    }
}
