using Common.Dtos.Prompt;
using Hushian.Application.Constants;
using Hushian.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hushian.WebApi.Controllers.v1
{
	[Route("api/v1/[controller]")]
	[ApiController]
	public class PromptController : ControllerBase
	{
		private readonly PromptService _promptService;
		public PromptController(PromptService promptService)
		{
			_promptService = promptService;
		}

		[HttpPost("AddPrompt")]
		[Authorize(Roles = "Company")]
		public async Task<ActionResult> AddPrompt([FromBody] ADD_PromptDto model)
		{
			string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
			int CompanyID = Convert.ToInt32(strCompanyID);
			var response = await _promptService.AddPrompt(model, CompanyID);
			return response.Success && response.Value ? NoContent()
				: BadRequest(response.Errors);
		}

		[HttpPut("UpdatePrompt")]
		[Authorize(Roles = "Company")]
		public async Task<ActionResult> UpdatePrompt([FromBody] ReadANDUpdate_PromptDto model)
		{
			string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
			int CompanyID = Convert.ToInt32(strCompanyID);
			var response = await _promptService.UpdatePrompt(model, CompanyID);
			return response.Success && response.Value ? NoContent()
				: BadRequest(response.Errors);
		}

		[HttpDelete("DeletePrompt/{id}")]
		[Authorize(Roles = "Company")]
		public async Task<ActionResult> DeletePrompt(int id)
		{
			string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
			int CompanyID = Convert.ToInt32(strCompanyID);
			var response = await _promptService.DeletePrompt(id, CompanyID);
			return response.Success && response.Value ? NoContent()
				: BadRequest(response.Errors);
		}

		[HttpGet("GetPrompt/{id}")]
		[Authorize(Roles = "Company")]
		public async Task<ActionResult<ReadANDUpdate_PromptDto>> GetPrompt(int id)
		{
			string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
			int CompanyID = Convert.ToInt32(strCompanyID);
			var response = await _promptService.GetPrompt(id, CompanyID);
			return response != null ? Ok(response) : NotFound();
		}

		[HttpGet("GetPrompts")]
		[Authorize(Roles = "Company")]
		public async Task<ActionResult<List<ReadANDUpdate_PromptDto>>> GetPrompts()
		{
			string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
			int CompanyID = Convert.ToInt32(strCompanyID);
			var response = await _promptService.GetCompanyPrompts(CompanyID);
			return Ok(response);
		}
	}
}


