<?php

namespace HelpGent\App\Providers;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\DTO\FormDTO;
use HelpGent\App\Services\BuilderMediaOptimizationService;
use HelpGent\WpMVC\Contracts\Provider;
use HelpGent\WpMVC\RequestValidator\Validator;
use WP_REST_Request;

class BuilderMediaOptimizationProvider implements Provider {
    public BuilderMediaOptimizationService $service;

    public function boot() {
        $this->service = helpgent_singleton( BuilderMediaOptimizationService::class );

        add_action( 'helpgent_after_create_form', [ $this, 'enqueue_from_form_dto' ] );
        add_action( 'helpgent_after_update_form', [ $this, 'enqueue_from_form_dto' ] );
        add_action( 'helpgent_before_form_auto_save', [ $this, 'enqueue_from_auto_save' ], 10, 3 );
        add_action( BuilderMediaOptimizationService::CRON_HOOK, [ $this, 'optimize_attachment' ] );
        add_filter( 'helpgent_form_with_builder_media_assets', [ $this, 'enrich_form' ] );
    }

    public function enqueue_from_form_dto( FormDTO $form_dto ): void {
        $this->service->enqueue_from_form_dto( $form_dto );
    }

    public function enqueue_from_auto_save( int $form_id, WP_REST_Request $request, Validator $validator ): void {
        $this->service->enqueue_from_auto_save_request( $request );
    }

    public function optimize_attachment( int $attachment_id ): void {
        $this->service->optimize_attachment( $attachment_id );
    }

    public function enrich_form( $form ) {
        return $this->service->enrich_form( $form );
    }
}
