<?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 HelpGent\WpMVC\View\View;
use WP_REST_Request;
use stdClass;

class PageController extends Controller {
    public FormRepository $form_repository;

    public function __construct( FormRepository $form_repository ) {
        $this->form_repository = $form_repository;
    }

    protected function render_page( stdClass $form, bool $is_preview = false, bool $disable_chat_bubbles = false ): void {
        include_once helpgent_dir( 'enqueues/register.php' );
        include_once helpgent_dir( 'enqueues/enqueue-common.php' );

        wp_enqueue_script( 'helpgent/form/shortcode' );

        if ( $disable_chat_bubbles ) {
            add_filter( 'helpgent_print_chat_bubbles', '__return_false' );
        }

        if ( $is_preview ) {
            $GLOBALS['helpgent_is_preview'] = true;
        } else {
            /**
             * @var QuizRepository $quiz_repository
             */
            $quiz_repository = helpgent_singleton( QuizRepository::class );
            $form            = $quiz_repository->sanitize_public_form( $form );
        }

        $is_preview = $is_preview ? 1 : 0;

        View::render( 'page', compact( 'form', 'is_preview' ) );
        die;
    }

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

        $show_preview = apply_filters( "helpgent_allowed_form_preview", current_user_can( 'manage_options' ), intval( $wp_rest_request->get_param( 'id' ) ) );

        if ( ! $show_preview ) {
            View::render(
                'form-preview-notice', [
                    'notice'            => esc_html__( "Sorry, you are not allowed to preview this form. Only Administrator can preview this form.", "helpgent" ),
                    'show_login_button' => true
                ] 
            );
            die;
        }

        $id = intval( $wp_rest_request->get_param( 'id' ) );

        $form = $this->form_repository->get_by_id( $id );

        if ( ! $form ) {
            View::render(
                'form-preview-notice', [
                    'notice' => esc_html__( "Sorry, form not found.", "helpgent" ),
                ] 
            );
            die;
        }

        $form = apply_filters( 'helpgent_form_with_builder_media_assets', helpgent_migrate_old_form( $form ) );

        $this->render_page( $form, true );
    }

    public function share( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'id' => 'required|numeric',
            ]
        );

        $id = intval( $wp_rest_request->get_param( 'id' ) );

        $form = $this->form_repository->get_by_id_publish( $id );

        if ( ! $form ) {
            View::render(
                'form-preview-notice', [
                    'notice' => esc_html__( "Sorry, form not found.", "helpgent" ),
                ]
            );
            die;
        }

        $this->render_page( $form, false, true );
    }

    public function share_by_slug( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'slug' => 'required|string',
            ]
        );

        $form = $this->form_repository->get_by_share_slug_publish(
            $wp_rest_request->get_param( 'slug' )
        );

        if ( ! $form ) {
            View::render(
                'form-preview-notice', [
                    'notice' => esc_html__( "Sorry, form not found.", "helpgent" ),
                ]
            );
            die;
        }

        $this->render_page( $form, false, true );
    }
}
