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

namespace survey_beta.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AnalyticsController : ControllerBase
    {
        private readonly AnalyticsServices _analyticsService;

        public AnalyticsController(AnalyticsServices analyticsService)
        {
            _analyticsService = analyticsService;
        }

        [HttpGet("{surveyId}/statistics")]
        public async Task<ActionResult<SurveyStats>> GetSurveyStatistics(string surveyId)
        {
            if (string.IsNullOrWhiteSpace(surveyId))
                return BadRequest(new { error = "Survey ID is required." });

            var statistics = await Task.Run(() => _analyticsService.GetAggregatedSurveyResponses(surveyId));

            if (statistics == null)
                return NotFound(new { error = "No data available for the given survey." });

            return Ok(statistics);
        }
        [HttpGet("{surveyId}/export")]
        public async Task<IActionResult> ExportSurveyStatistics(string surveyId)
        {
            if (string.IsNullOrWhiteSpace(surveyId))
                return BadRequest(new { error = "Survey ID is required." });
            var fileResult = await _analyticsService.ExportResponsesToCsv(surveyId);

            if (fileResult == null)
                return NotFound(new { error = "No data available to export." });

            return fileResult;
        }
    }
}
