<?php

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

/**
 * Enqueue block editor assets for Gutenberg blocks.
 * Runs only inside the block editor (admin), not on the front end.
 */
function irep_enqueue_block_editor_assets()
{
    $dependencies = [
        'wp-blocks',
        'wp-block-editor',
        'wp-components',
        'wp-element',
        'wp-i18n',
    ];

    $scripts = [
        'irep-flats-block'   => 'irep-flats-block.js',
        'irep-project-block' => 'irep-project-block.js',
    ];

    foreach ($scripts as $handle => $filename) {
        $js_path = __DIR__ . '/' . $filename;
        if (!file_exists($js_path)) {
            continue;
        }

        wp_enqueue_script(
            $handle,
            plugins_url($filename, __FILE__),
            $dependencies,
            filemtime($js_path),
            true
        );

        wp_localize_script($handle, 'irepBlockData', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce'    => wp_create_nonce('irep_nonce'),
        ]);
    }
}

add_action('enqueue_block_editor_assets', 'irep_enqueue_block_editor_assets');


/**
 * Render callback for the dynamic irep_flats block.
 * Maps Gutenberg block attributes back to the existing shortcode handler.
 *
 * @param array $attrs Block attributes saved by the editor.
 * @return string      HTML output from irep_render_flats_shortcode().
 */
function irep_render_flats_block(array $attrs): string
{
    $shortcode_atts = [
        'id'       => isset($attrs['projectId']) ? intval($attrs['projectId']) : 0,
        'per_page' => isset($attrs['perPage'])   ? intval($attrs['perPage'])   : 7,
        'block_id' => isset($attrs['blockId'])   ? sanitize_text_field((string) $attrs['blockId']) : '',
        'layout'   => isset($attrs['layout'])    ? sanitize_text_field($attrs['layout']) : 'mixed',
        'order_by' => '',
    ];

    return irep_render_flats_shortcode($shortcode_atts);
}

/**
 * Render callback for the dynamic irep_project block.
 * Maps Gutenberg block attributes back to the existing shortcode handler.
 *
 * @param array $attrs Block attributes saved by the editor.
 * @return string      HTML output from irep_render_building_render_shortcode().
 */
function irep_render_project_block(array $attrs): string
{
    $shortcode_atts = [
        'id' => isset($attrs['projectId']) ? intval($attrs['projectId']) : 0,
    ];

    return irep_render_building_render_shortcode($shortcode_atts);
}


/**
 * Register Gutenberg blocks as server-side dynamic blocks.
 */
function irep_register_blocks()
{
    if (!function_exists('register_block_type')) {
        return;
    }

    register_block_type('interactive-real-estate/flats', [
        'editor_script'   => 'irep-flats-block',
        'render_callback' => 'irep_render_flats_block',
        'attributes'      => [
            'projectId' => [
                'type'    => 'number',
                'default' => 0,
            ],
            'perPage'   => [
                'type'    => 'number',
                'default' => 7,
            ],
            'blockId'   => [
                'type'    => 'string',
                'default' => '',
            ],
            'layout'    => [
                'type'    => 'string',
                'default' => 'mixed',
            ],
        ],
    ]);

    register_block_type('interactive-real-estate/project', [
        'editor_script'   => 'irep-project-block',
        'render_callback' => 'irep_render_project_block',
        'attributes'      => [
            'projectId' => [
                'type'    => 'number',
                'default' => 0,
            ],
        ],
    ]);
}

add_action('init', 'irep_register_blocks');
