<?php

namespace App\Customizer;

function turbo_blocks_get_upgrade_url(): string
{
    $url = '';

    if (defined('TURBO_BLOCKS_UPGRADE_URL')) {
        $url = (string) TURBO_BLOCKS_UPGRADE_URL;
    }

    $url = (string) apply_filters('turbo_blocks_upgrade_url', $url);

    return esc_url_raw($url);
}

function turbo_blocks_render_pro_notice(string $message): string
{
    $upgrade_url = turbo_blocks_get_upgrade_url();

    $html = '<p style="margin:12px 0 0;padding:10px 12px;border-left:4px solid #f97316;background:#111827;color:#f9fafb;">'
        . esc_html($message)
        . '</p>';

    if ($upgrade_url !== '') {
        $html .= '<p style="margin:8px 0 0;">'
            . '<a class="button button-primary" href="' . esc_url($upgrade_url) . '" target="_blank" rel="noopener noreferrer">'
            . esc_html__('Upgrade to Turbo Blocks Pro', 'turbo')
            . '</a>'
            . '</p>';
    }

    return $html;
}

/**
 * Pro-only Customizer gating map.
 *
 * These IDs are consumed by Turbo Blocks plugin filters:
 * - turbo_blocks_pro_customizer_controls
 * - turbo_blocks_pro_customizer_sections
 * - turbo_blocks_pro_customizer_panels
 *
 * Keep this focused on controls you want hidden in Free.
 */

add_filter('turbo_blocks_pro_customizer_controls', function ($controls) {
    $pro_controls = [
        // Advanced spacing controls.
        'hayden_spacing_scale_advanced_control',
        'hayden_spacing_scale_intro_control',
        'hayden_spacing_value_small_control',
        'hayden_spacing_value_medium_control',
        'hayden_spacing_value_large_control',
        'hayden_spacing_scale_reset_control',

        // Advanced block inner spacing controls (Top/Bottom).
        'hayden_block_padding_scale_intro_control',
        'hayden_block_padding_value_small_control',
        'hayden_block_padding_value_medium_control',
        'hayden_block_padding_value_large_control',
        'hayden_block_padding_scale_reset_control',

        // Advanced block inner spacing controls (Left/Right).
        'hayden_block_padding_x_scale_intro_control',
        'hayden_block_padding_x_value_small_control',
        'hayden_block_padding_x_value_medium_control',
        'hayden_block_padding_x_value_large_control',
        'hayden_block_padding_x_scale_reset_control',

        // Pro typography controls.
        'hayden_font_serif_file',
        'hayden_font_sans_file',

        // Border radius scale controls.
        'hayden_radius_scale_intro_control',
        'hayden_radius_soft_scale_control',
        'hayden_radius_round_scale_control',
        'hayden_radius_scale_reset_control',

        // Cards & grid advanced controls.
        'grid_projects_initial_items_control',
        'grid_blog_initial_items_control',
    ];

    return array_values(array_unique(array_merge((array) $controls, $pro_controls)));
});

add_filter('turbo_blocks_pro_customizer_sections', function ($sections) {
    $pro_sections = [
        // Add section IDs here if you decide to hide full sections in Free.
    ];

    return array_values(array_unique(array_merge((array) $sections, $pro_sections)));
});

add_filter('turbo_blocks_pro_customizer_panels', function ($panels) {
    $pro_panels = [
        // Add panel IDs here if you decide to hide full panels in Free.
    ];

    return array_values(array_unique(array_merge((array) $panels, $pro_panels)));
});

/**
 * Add simple upgrade notices into sections where Pro-only controls are hidden.
 */
add_action('customize_register', function (\WP_Customize_Manager $wp_customize) {
    $is_pro_active = function_exists('turbo_blocks_is_pro_active') && turbo_blocks_is_pro_active();

    if ($is_pro_active) {
        return;
    }

    if ($wp_customize->get_section('hayden_spacing_section')) {
        $wp_customize->add_setting('turbo_blocks_pro_notice_spacing', [
            'sanitize_callback' => '__return_null',
        ]);

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            'turbo_blocks_pro_notice_spacing_control',
            [
                'section'     => 'hayden_spacing_section',
                'settings'    => 'turbo_blocks_pro_notice_spacing',
                'type'        => 'custom',
                'priority'    => 999,
                'description' => turbo_blocks_render_pro_notice(__('Unlock advanced spacing controls in Turbo Blocks Pro.', 'turbo')),
            ]
        ));
    }

    if ($wp_customize->get_section('hayden_typography_section')) {
        $wp_customize->add_setting('turbo_blocks_pro_notice_typography', [
            'sanitize_callback' => '__return_null',
        ]);

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            'turbo_blocks_pro_notice_typography_control',
            [
                'section'     => 'hayden_typography_section',
                'settings'    => 'turbo_blocks_pro_notice_typography',
                'type'        => 'custom',
                'priority'    => 999,
                'description' => turbo_blocks_render_pro_notice(__('Unlock custom font upload options in Turbo Blocks Pro.', 'turbo')),
            ]
        ));
    }

    if ($wp_customize->get_section('hayden_design_section')) {
        $wp_customize->add_setting('turbo_blocks_pro_notice_radius', [
            'sanitize_callback' => '__return_null',
        ]);

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            'turbo_blocks_pro_notice_radius_control',
            [
                'section'     => 'hayden_design_section',
                'settings'    => 'turbo_blocks_pro_notice_radius',
                'type'        => 'custom',
                'priority'    => 999,
                'description' => turbo_blocks_render_pro_notice(__('Unlock border radius scale values in Turbo Blocks Pro.', 'turbo')),
            ]
        ));
    }

    if ($wp_customize->get_section('hayden_cards_grid_section')) {
        $wp_customize->add_setting('turbo_blocks_pro_notice_cards_grid', [
            'sanitize_callback' => '__return_null',
        ]);

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            'turbo_blocks_pro_notice_cards_grid_control',
            [
                'section'     => 'hayden_cards_grid_section',
                'settings'    => 'turbo_blocks_pro_notice_cards_grid',
                'type'        => 'custom',
                'priority'    => 999,
                'description' => turbo_blocks_render_pro_notice(__('Unlock cards and grid initial item count controls in Turbo Blocks Pro.', 'turbo')),
            ]
        ));
    }
}, 1001);
