<?php

namespace HelpGent\App\Providers;

defined( "ABSPATH" ) || exit;

use HelpGent\WpMVC\Contracts\Provider;
use HelpGent\App\Repositories\ResponseRepository;
use HelpGent\App\Repositories\FormRepository;

class ResponseMetaDataProvider implements Provider {
    public function boot() {
        add_action( 'helpgent_after_response_created', [ $this, 'after_response_created' ], 10, 2 );
    }

    public function after_response_created( int $response_id, array $data ) {
        /**
         * @var FormRepository $form_repository
         */
        $form_repository = helpgent_singleton( FormRepository::class );

        $form = $form_repository->get_by_id( $data['form_id'] );

        if ( ! $form ) {
            return;
        }

        /**
         * @var ResponseRepository
         */
        $response_repository = helpgent_singleton( ResponseRepository::class );

        $response_repository->add_meta( $response_id, 'form_content', $form->content );
    }
}