<?php

namespace HelpGent\App\Http\Controllers;

defined( 'ABSPATH' ) || exit;

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

class QuizController extends Controller {
    public QuizRepository $quiz_repository;

    public AnswerRepository $answer_repository;

    public function __construct( QuizRepository $quiz_repository, AnswerRepository $answer_repository ) {
        $this->quiz_repository   = $quiz_repository;
        $this->answer_repository = $answer_repository;
    }

    public function result( Validator $validator, WP_REST_Request $request ) {
        $validator->validate(
            [
                'form_id'        => 'required|numeric',
                'response_token' => 'required|string',
                'element_id'     => 'string',
            ]
        );

        $form_id   = intval( $request->get_param( 'form_id' ) );
        $response  = helpgent_get_response_by_token( $request->get_param( 'response_token' ), $form_id );
        $element_id = $request->get_param( 'element_id' );

        if ( ! $response ) {
            throw new Exception( esc_html__( 'Response not found.', 'helpgent' ), 404 );
        }

        return Response::send(
            [
                'quiz'    => $this->quiz_repository->get_response_summary( intval( $response->id ) ),
                'answers' => $this->quiz_repository->get_review_answers( intval( $response->id ), $element_id ),
            ]
        );
    }

    public function feedback( Validator $validator, WP_REST_Request $request ) {
        $validator->validate(
            [
                'form_id'        => 'required|numeric',
                'element_id'     => 'required|string',
                'response_token' => 'string',
            ]
        );

        return Response::send(
            [
                'feedback' => $this->quiz_repository->get_feedback_result(
                    intval( $request->get_param( 'form_id' ) ),
                    strval( $request->get_param( 'element_id' ) ),
                    $request->get_param( 'value' ),
                    $request->get_param( 'response_token' )
                ),
            ]
        );
    }

    public function update_points( Validator $validator, WP_REST_Request $request ) {
        $validator->validate(
            [
                'form_id'     => 'required|numeric',
                'response_id' => 'required|numeric',
                'element_id'  => 'required|string',
                'points'      => 'required|numeric',
            ]
        );

        $form_id     = intval( $request->get_param( 'form_id' ) );
        $response_id = intval( $request->get_param( 'response_id' ) );
        $element_id  = $request->get_param( 'element_id' );
        $points      = intval( $request->get_param( 'points' ) );

        $data = $this->quiz_repository->update_manual_points(
            $form_id,
            $response_id,
            $element_id,
            $points
        );

        $answer = null;

        foreach ( $this->answer_repository->get( $response_id ) as $item ) {
            if ( $item->element_id === $element_id ) {
                $answer = $item;
                break;
            }
        }

        return Response::send(
            [
                'quiz'    => $data['quiz'],
                'answer'  => $answer,
                'message' => esc_html__( 'Quiz points updated successfully.', 'helpgent' ),
            ]
        );
    }
}
