using HanyadikHetVan.DTO;
using HanyadikHetVan.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net.Mime;
using System.Threading.Tasks;
using System.Security.Claims;

namespace HanyadikHetVan.Controllers.V2
{
    [ApiVersion("2.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [ApiController]
    public class PauseController : Controller
    {
        private readonly PauseService _pauseService;

        public PauseController(PauseService pauseService)
        {
            _pauseService = pauseService;
        }
        [HttpPost]
        [Authorize]
        [Consumes(MediaTypeNames.Application.Json)]
        [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(PauseDTO))]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
        public async Task<IActionResult> AddPause([FromBody] PauseDTO pause)
        {
            try
            {
                var obj = await _pauseService.AddPause(this.User.FindFirst(ClaimTypes.NameIdentifier).Value, pause);
                return CreatedAtAction(nameof(GetPauseById),new { pauseId = obj.Id } ,obj);
            }
            catch (Exception)
            {
                return BadRequest();
            }
        }
        [HttpDelete("{pauseId}")]
        [Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
        public async Task<IActionResult> DeletePause(int pauseId)
        {
            var result = await _pauseService.DeletePause(pauseId);
            if (result)
            {
                return Ok(result);
            }
            else
            {
                return NotFound();
            }
        }
        [HttpPut]
        [Authorize(Policy = "AdminPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        [Consumes(MediaTypeNames.Application.Json)]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PauseDTO))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
        public async Task<IActionResult> UpdatePause([FromBody] PauseDTO pause)
        {
            try
            {
                var obj = await _pauseService.UpdatePause(pause);
                return Ok(obj);
            }
            catch (Exception)
            {
                return NotFound();
            }
        }
        [HttpGet]
        public async Task<List<PauseDTO>> GetAllPauses()
        {
            return await _pauseService.GetAllPauses();
        }

        [HttpGet("{pauseId}")]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PauseDTO))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> GetPauseById(int pauseId)
        {
            var pause = await _pauseService.GetPause(pauseId);
            if (pause == null)
            {
                return NotFound();
            }
            else
            {
                return Ok(pause);
            }
        }
    }
}
