using Microsoft.AspNetCore.Authorization;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MTWorkHR.Application.Filters;
using MTWorkHR.Application.Identity;
using MTWorkHR.Application.Models;
using MTWorkHR.Application.Services.Interfaces;
using MTWorkHR.Identity.Services;

namespace MTWorkHR.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [AppAuthorize]
    public class UserTaskController : ControllerBase
    {
        private readonly IUserTaskService _userTaskService;
        private readonly IUserTaskAttachmentService _attachmentService;
        private readonly IUserTaskHistoryService _taskHistoryService;
        public UserTaskController(IUserTaskService userUserTaskService, IUserTaskAttachmentService attachmentService, IUserTaskHistoryService taskHistoryService)
        {
            this._userTaskService = userUserTaskService;
            _attachmentService = attachmentService;
            _taskHistoryService = taskHistoryService;
        }
        [HttpGet("GetAll")]
        [AppAuthorize(Permissions = "UserTask")]
        public async Task<ActionResult<List<UserTaskAllDto>>> GetAll([FromQuery]UserTaskPagingInputDto pagingInput)
        {
            return Ok(await _userTaskService.GetAll(pagingInput));
        }
        [HttpGet("Get")]
        [AppAuthorize(Permissions = "UserTask")]
        public async Task<ActionResult<UserTaskDto>> Get(long UserTaskId)
        {
            return Ok(await _userTaskService.GetById(UserTaskId));
        }


        [HttpPost("Create")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Create")]
        public async Task<ActionResult<UserTaskDto>> Create([FromBody] UserTaskDto input)
        {
            return await _userTaskService.Create(input);
        }

        [HttpPost("Update")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Update")]
        public async Task<ActionResult<UserTaskDto>> Update([FromBody] UserTaskDto input)
        {
            return await _userTaskService.Update(input);
        }

        [HttpDelete("Delete")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Delete")]
        public async Task Delete([FromQuery] long id)
        {
            await _userTaskService.Delete(id);
        }

        #region attachments
        [HttpPost("CreateAttachment")]
        [ProducesResponseType(StatusCodes.Status200OK)]
       // [Consumes("multipart/form-data")]
        [AppAuthorize(Permissions = "UserTask.Create")]
        public async Task<ActionResult<AttachmentDto>> CreateAttachment([FromBody] AttachmentDto input)
        {
            return await _attachmentService.Create(input);
        }
        [HttpPost("UpdateAttachment")]
        [ProducesResponseType(StatusCodes.Status200OK)]
       // [Consumes("multipart/form-data")]
        [AppAuthorize(Permissions = "UserTask.Update")]
        public async Task<ActionResult<AttachmentDto>> UpdateAttachment([FromBody] AttachmentDto input)
        {
            return await _attachmentService.Update(input);
        }

        [HttpDelete("DeleteAttachment")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Delete")]
        public async Task DeleteAttachment([FromQuery] long id)
        {
            await _attachmentService.Delete(id);
        }

        #endregion

        #region History
        [HttpPost("CreateTaskHistory")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Create")]
        public async Task<ActionResult<UserTaskHistoryDto>> CreateTaskHistory([FromBody] UserTaskHistoryDto input)
        {
            return await _taskHistoryService.Create(input);
        }

        [HttpPost("UpdateTaskHistory")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Update")]
        public async Task<ActionResult<UserTaskHistoryDto>> UpdateTaskHistory([FromBody] UserTaskHistoryDto input)
        {
            return await _taskHistoryService.Update(input);
        }
        [HttpDelete("DeleteTaskHistory")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Delete")]
        public async Task DeleteTaskHistory([FromQuery]long id)
        {
            await _taskHistoryService.Delete(id);
        }
        [HttpPost("ChangeStatus")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "UserTask.Update")]
        public async Task ChangeStatus([FromBody] TaskStatusDto input)
        {
            await _userTaskService.ChangeStatus(input.TaskId, input.StatusId);
        }
        #endregion

    }
}
