using Microsoft.AspNetCore.Mvc;
using survey_beta.DTOs.Response;
using survey_beta.Models;
using System;
using System.Threading.Tasks;

namespace survey_beta.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ResponseController : ControllerBase
    {
        private readonly ResponsesService _responsesService;

        public ResponseController(ResponsesService responsesService)
        {
            _responsesService = responsesService;
        }

        [HttpPost("add")]
        public async Task<IActionResult> CreateResponse([FromBody] ResponseDto request)
        {
            try
            {
                await _responsesService.AddResponseAsync(request);
                return Ok(new { message = "Response added successfully." });
            }
            catch (Exception ex)
            {
                return BadRequest(new { error = ex.Message });
            }
        }
        [HttpGet("survey/{surveyId}")]
        public async Task<IActionResult> GetSurveyResponses(string surveyId)
        {
            var responses = await _responsesService.GetSurveyResponsesAsync(surveyId);
            if (responses == null || responses.Count == 0)
            {
                return NotFound(new { message = "No responses found for this survey." });
            }
            return Ok(responses);
        }
    }
}
