<?php

namespace HelpGent\App\Http\Controllers;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Repositories\AnswerRepository;
use HelpGent\App\Repositories\ContactInfoRepository;
use HelpGent\App\Repositories\QuizRepository;
use HelpGent\WpMVC\RequestValidator\Validator;
use HelpGent\WpMVC\Routing\Response;
use WP_REST_Request;

class AnswerController extends Controller {
    public AnswerRepository $repository;

    public ContactInfoRepository $contact_info_repository;

    public function __construct( AnswerRepository $repository, ContactInfoRepository $contact_info_repository ) {
        $this->repository              = $repository;
        $this->contact_info_repository = $contact_info_repository;
    }

    public function index( Validator $validator, WP_REST_Request $request ) {
        $validator->validate(
            [
                'response_id' => 'required|numeric'
            ]
        );

        $response_id = intval( $request->get_param( 'response_id' ) );
        /**
         * @var QuizRepository $quiz_repository
         */
        $quiz_repository = helpgent_singleton( QuizRepository::class );

        return Response::send(
            [
                'contact_info' => $this->contact_info_repository->get_response_contact_info( $response_id ),
                'answers'      => $this->repository->get( $response_id ),
                'quiz'         => $quiz_repository->get_response_summary( $response_id ),
            ] 
        );
    }
}

