<?php

namespace Better_Payment\Lite\Campaign;

use Better_Payment\Lite\Controller;
use Better_Payment\Lite\Campaign\Services\RendererService;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Registers the better-payment/campaign-display Gutenberg block.
 * Server-side rendered — delegates to RendererService::render_campaign().
 * Follows the same manual asset registration pattern as BlockManager.
 */
class CampaignBlock extends Controller {

    public function register() {
        if ( ! function_exists( 'register_block_type' ) ) {
            return;
        }

        $assets_dir = BETTER_PAYMENT_PATH . '/assets/blocks/campaign-display/';
        $src_dir    = BETTER_PAYMENT_PATH . '/src/blocks/campaign-display/';

        // Load dependency manifest generated by the blocks webpack build.
        $asset_file = file_exists( $assets_dir . 'index.min.asset.php' )
            ? require $assets_dir . 'index.min.asset.php'
            : [ 'dependencies' => [], 'version' => BETTER_PAYMENT_VERSION ];

        // Register editor script from compiled assets.
        $script_src = file_exists( $assets_dir . 'index.min.js' )
            ? BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/index.min.js'
            : null;

        if ( $script_src ) {
            wp_register_script(
                'better-payment-campaign-display-editor',
                $script_src,
                $asset_file['dependencies'],
                $asset_file['version'],
                true
            );
        }

        // Register frontend style from compiled assets.
        $style_src = file_exists( $assets_dir . 'style.min.css' )
            ? BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/style.min.css'
            : null;

        if ( $style_src ) {
            wp_register_style(
                'better-payment-campaign-display-style',
                $style_src,
                [],
                $asset_file['version']
            );
        }

        // block.json lives in src/ — fall back gracefully in dev.
        $block_json = file_exists( $src_dir . 'block.json' ) ? $src_dir : null;
        if ( ! $block_json ) {
            return;
        }

        register_block_type(
            $block_json,
            [
                'render_callback' => [ $this, 'render' ],
                'editor_script'   => 'better-payment-campaign-display-editor',
                'style'           => 'better-payment-campaign-display-style',
            ]
        );
    }

    /**
     * Server-side render callback.
     *
     * @param array $attributes  Block attributes from block.json.
     * @return string
     */
    public function render( array $attributes ): string {
        $campaign_id = absint( $attributes['campaignId'] ?? 0 );
        if ( ! $campaign_id ) {
            return '<p class="bp-campaign-placeholder">' . esc_html__( 'Please select a campaign.', 'better-payment' ) . '</p>';
        }

        $post = get_post( $campaign_id );
        if ( ! $post || $post->post_type !== 'bp_campaign' ) {
            return '';
        }

        if ( ! wp_script_is( 'fundraising-campaign-script', 'enqueued' ) ) {
            wp_enqueue_script( 'fundraising-campaign-script' );
        }

        return RendererService::render_campaign( $campaign_id );
    }
}
