using AutoMapper;
using Microsoft.EntityFrameworkCore;
using survey_beta.DataBaseContext;
using survey_beta.DTOs.Response;
using survey_beta.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class ResponsesService
{
    private readonly AppDbContext _context;
    private readonly IMapper _mapper;

    public ResponsesService(AppDbContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }
    public async Task AddResponseAsync(ResponseDto responseDto)
    {
        var response = new Response
        {
            Id = responseDto.Id,
            SurveyId = responseDto.SurveyId,
            IpAddress = responseDto.IpAddress,
            CreatedAt = DateTime.UtcNow,
        };

        foreach (var answer in responseDto.Answers)
        {
            var answerEntity = new Answer
            {
                QuestionId = answer.QuestionId,
                ResponseId = response.Id
            };
            response.Answers.Add(answerEntity);
        }

        await _context.Responses.AddAsync(response);
        await _context.SaveChangesAsync();
    }
    public async Task<List<Response>> GetSurveyResponsesAsync(string surveyId)
    {
        return await _context.Responses
            .Where(r => r.SurveyId == surveyId)
            .Include(r => r.Answers)
            .ToListAsync();
    }
}