<?php

namespace HelpGent\App\Http\Controllers;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Http\Controllers\Controller;
use HelpGent\App\Repositories\FormRepository;
use HelpGent\App\Repositories\QuizRepository;
use HelpGent\WpMVC\RequestValidator\Validator;
use HelpGent\WpMVC\Routing\Response;
use WP_REST_Request;

class FormBubbleController extends Controller {
    public function index( Validator $validator, WP_REST_Request $wp_rest_request, FormRepository $form_repository ) {
        $validator->validate(
            [
                'page_id' => 'required|numeric',
            ]
        );
        $forms = $form_repository->get_bubble_by_page_id( intval( $wp_rest_request->get_param( 'page_id' ) ) );
        /**
         * @var QuizRepository $quiz_repository
         */
        $quiz_repository = helpgent_singleton( QuizRepository::class );

        $forms = array_map(
            function( $form ) use ( $quiz_repository ) {
                return $quiz_repository->sanitize_public_form( $form );
            },
            $forms
        );

        return Response::send(
            [
                'forms' => $forms
            ]
        );
    }
}
