using AutoMapper;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.DTOs.Customer;
using Diligent.WebAPI.Host.DTOs.Request;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Request.Commands;
using Diligent.WebAPI.Host.Mediator.Request.Queries;
using MediatR;
using Microsoft.AspNetCore.Mvc;

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

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

        [HttpGet]
        public async Task<List<Request>> GetRequests() =>
            await _mediator.Send(new GetAllRequestsQuery());

        [HttpGet("{id}")]
        public async Task<Request> GetRequestById(string id) =>
            await _mediator.Send(new GetRequestQuery(id));

        // get all rooms for which exist at least one request from customer
        [HttpGet("request-rooms")]
        public async Task<ActionResult<List<RequestRoomReadDTO>>> GetRoomsForWhichRequestExist() =>
            await _mediator.Send(new GetRoomsForWhichRequestExistQuery());

        [HttpGet("room-customers")]
        public async Task<ActionResult<List<CustomerRequestRoomReadDTO>>> GetCustomersForSpecificRequestRoom(string id) =>
            await _mediator.Send(new GetSendersForSpecificRequestRoomQuery(id));

        [HttpPost("accept-request")]
        public async Task<ActionResult> AcceptCustomerRequest(AcceptCustomerDTO acceptCustomerDTO)
        {
            var id = await _mediator.Send(new AcceptCustomerRequestCommand(acceptCustomerDTO.customerId, acceptCustomerDTO.roomId));
            return Ok(id);
        }

        [HttpPost("reject-request")]
        public async Task<ActionResult> RejectCustomerRequest(AcceptCustomerDTO acceptCustomerDTO)
        {
            var id = await _mediator.Send(new RejectCustomerRequestCommand(acceptCustomerDTO.customerId, acceptCustomerDTO.roomId));
            return Ok(id);
        }
 
        [HttpPost("join-request")]
        public async Task<IActionResult> CreateRequest(RequestCreateDTO req)
        {
            var request = await _mediator.Send(new CreateRequestCommand(req));

            return CreatedAtAction(nameof(RequestController.CreateRequest), new { id = request.Id }, request);
        }

    }
}
