<?php

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



/**
 * Enqueue Vue.js assets only on the front-end of the site.
 */
function irep_enqueue_frontend_scripts()
{

    $plugin_dir = plugin_dir_path(IREP_PLUGIN_FILE);
    $css_file   = $plugin_dir . 'dist-shortcode/assets/index.css';
    $js_file    = $plugin_dir . 'dist-shortcode/assets/index.js';
    $base_ver   = defined('IREP_PLUGIN_VERSION') ? IREP_PLUGIN_VERSION : '1.0.0';
    $css_ver    = file_exists($css_file) ? $base_ver . '.' . filemtime($css_file) : $base_ver;
    $js_ver     = file_exists($js_file)  ? $base_ver . '.' . filemtime($js_file)  : $base_ver;

    wp_enqueue_style(
        'irep-shortcode-style',
        plugin_dir_url(IREP_PLUGIN_FILE) . 'dist-shortcode/assets/index.css',
        [],
        $css_ver
    );

    wp_enqueue_script(
        'irep-shortcode',
        plugin_dir_url(IREP_PLUGIN_FILE) . 'dist-shortcode/assets/index.js',
        [],
        $js_ver,
        true
    );

    $translations = irep_get_translations();

    $frontend_data = array(
        'nonce' => wp_create_nonce('irep_nonce'),
        'ajax_url' => admin_url('admin-ajax.php'),
        'translations' =>  $translations,
        'is_premium' => ire_fs()->can_use_premium_code(),
        'is_gold' => ire_fs()->is_plan_or_trial('gold', true),
        /** Price history add-on (Freemius slug ire-poland-compliance). */
        'price_history_addon' => irep_is_price_history_addon_active(),
        'building_360_addon'  => irep_is_building_360_addon_active(),
    );

    // Provide both global object properties and plain var for maximum compatibility.
    wp_add_inline_script(
        'irep-shortcode',
        'window.irePluginWp=' . wp_json_encode($frontend_data) . ';globalThis.irePluginWp=window.irePluginWp;var irePluginWp=window.irePluginWp;',
        'before'
    );
}

add_action('wp_enqueue_scripts', 'irep_enqueue_frontend_scripts', 20);




/**
 * Render the shortcode to display a Vue component with dynamic project ID.
 *
 * @param array $atts Shortcode attributes, including the project ID.
 *
 * @return string The HTML markup for the Vue component.
 */
function irep_render_building_render_shortcode($atts)
{

    // Set default attribute values for the shortcode
    $atts = shortcode_atts(array(
        'id' => 0, // Default to 0 if no ID is provided
        'view' => '1', // 1-based view index (1 = primary)
    ), $atts);

    // Get and sanitize the project ID and view index
    $project_id = intval($atts['id']);
    $view = max(1, intval($atts['view']));
    // Generate a unique ID for the div container to prevent conflicts
    $unique_id = 'irep-shortcode-' . uniqid();

    ob_start(); // Start output buffering
?>
    <div id="<?php echo esc_attr($unique_id); ?>" data-project-id="<?php echo esc_attr($project_id); ?>" data-view="<?php echo esc_attr($view); ?>">
    </div>
    <?php
    return ob_get_clean(); // Return the generated HTML content
}

add_shortcode('irep_project', 'irep_render_building_render_shortcode');

/**
 * Renders the container for the single-block shortcode [irep_block id="block_id"].
 * The block id is globally unique; the frontend resolves the owning project from it.
 *
 * @param array $atts Shortcode attributes.
 * @return string The HTML markup for the Vue component.
 */
function irep_render_block_shortcode($atts)
{
    // Set default attribute values for the shortcode
    $atts = shortcode_atts(array(
        'id' => 0, // Block id (default to 0 if none provided)
    ), $atts);

    // Get and sanitize the block ID
    $block_id = intval($atts['id']);
    // Generate a unique ID for the div container to prevent conflicts
    $unique_id = 'irep-shortcode-block-' . uniqid();

    ob_start(); // Start output buffering
?>
    <div id="<?php echo esc_attr($unique_id); ?>" data-block-id="<?php echo esc_attr($block_id); ?>">
    </div>
    <?php
    return ob_get_clean(); // Return the generated HTML content
}

add_shortcode('irep_block', 'irep_render_block_shortcode');

function irep_render_flats_shortcode($atts)
{
    // Set default attribute values for the shortcode
    $atts = shortcode_atts(array(
        'id' => 0, // Default to 0 if no ID is provided
        'per_page' => 7,
        'layout' => 'mixed',
        'default_view' => 'grid',
        'block_id' => '',
        'order_by' => '',
    ), $atts);

    // Get and sanitize the project ID
    $project_id = intval($atts['id']);
    $per_page = intval($atts['per_page']);
    $layout = $atts['layout'];
    $default_view = $atts['default_view'];
    $block_id = $atts['block_id'];
    $orderby = $atts['order_by'];


    // Generate a unique ID for the div container to prevent conflicts
    $unique_id = 'irep-shortcode-flats-' . uniqid();

    // Check for gold license
    $isGold = ire_fs()->is_plan_or_trial('gold', true);

    ob_start(); // Start output buffering

    if ($isGold) {
        // Render div with specified attributes if gold license exists
    ?>
        <div id="<?php echo esc_attr($unique_id); ?>"
            data-project-id-flats="<?php echo esc_attr($project_id); ?>"
            per-page="<?php echo esc_attr($per_page); ?>"
            layout="<?php echo esc_attr($layout); ?>"
            default-view="<?php echo esc_attr($default_view); ?>"
            block-id="<?php echo esc_attr($block_id); ?>"
            order-by="<?php echo esc_attr($orderby); ?>">

        </div>
    <?php
    } else {
        // Render div with message if no gold license
    ?>
        <div>
            <?php echo esc_html__('A Gold license is required to display this content.', 'your-text-domain'); ?>
        </div>
<?php
    }

    return ob_get_clean(); // Return the generated HTML content
}

add_shortcode('irep_flats', 'irep_render_flats_shortcode');





function irep_get_nonce()
{
    wp_send_json_success([
        'nonce' => wp_create_nonce('irep_nonce')
    ]);
}


add_action('wp_ajax_irep_get_nonce', 'irep_get_nonce');
add_action('wp_ajax_nopriv_irep_get_nonce', 'irep_get_nonce');
