<?php
/**
 * Simple Pricing Table block.
 *
 * A stripped-down sibling of hp-pricing-table: a single 3-column table
 * (plan name | annual price + sub-line | Get-Started button), driven by
 * an ACF repeater. No Monthly/Annual toggle, no feature icons, no
 * Enterprise card — built for short pricing pages and embedded blocks.
 */

// Pre-fill the Rows repeater on a freshly-inserted block so the editor
// shows a canonical example layout instead of an empty repeater.
add_filter('acf/load_value/key=field_hp_spt_rows', function ($value, $post_id, $field) {
	if (!empty($value)) {
		return $value;
	}
	if (!is_string($post_id) || strpos($post_id, 'block_') !== 0) {
		return $value;
	}
	return [
		[
			'plan_label'  => '0 – 500',
			'price'       => '$579',
			'sub_text'    => '≈ $48/mo, billed annually',
			'button_text' => 'Get Started',
			'button_url'  => '',
		],
		[
			'plan_label'  => '501 – 1,000',
			'price'       => '$779',
			'sub_text'    => '≈ $65/mo, billed annually',
			'button_text' => 'Get Started',
			'button_url'  => '',
		],
		[
			'plan_label'  => '1,001 – 1,500',
			'price'       => '$959',
			'sub_text'    => '≈ $80/mo, billed annually',
			'button_text' => 'Get Started',
			'button_url'  => '',
		],
		[
			'plan_label'  => '1,501 – 2,500',
			'price'       => '$1,199',
			'sub_text'    => '≈ $100/mo, billed annually',
			'button_text' => 'Get Started',
			'button_url'  => '',
		],
		[
			'plan_label'  => '2,501 – 5,000',
			'price'       => '$1,499',
			'sub_text'    => '≈ $125/mo, billed annually',
			'button_text' => 'Get Started',
			'button_url'  => '',
		],
	];
}, 10, 3);

add_action('acf/init', function () {
	if (!function_exists('acf_register_block_type')) {
		return;
	}

	acf_register_block_type([
		'name'            => 'hp-simple-pricing-table',
		'title'           => __('Simple Pricing Table'),
		'description'     => __('Compact 3-column pricing table (plan, price, button). Repeater-driven, no toggle.'),
		'render_callback' => 'bw_render_acf_block',
		'category'        => 'design',
		'icon'            => 'list-view',
		'keywords'        => ['pricing', 'plans', 'stripe', 'table', 'simple'],
		'mode'            => 'preview',
		'supports'        => [
			'align' => ['wide', 'full'],
			'jsx'   => true,
		],
	]);
});

add_action('acf/init', function () {
	if (!function_exists('acf_add_local_field_group')) {
		return;
	}

	acf_add_local_field_group([
		'key'      => 'group_hp_simple_pricing_table',
		'title'    => 'Simple Pricing Table',
		'location' => [[[
			'param'    => 'block',
			'operator' => '==',
			'value'    => 'acf/hp-simple-pricing-table',
		]]],
		'fields'   => [
			[
				'key'           => 'field_hp_spt_header_plan',
				'label'         => 'Header: Plan column',
				'name'          => 'header_plan',
				'type'          => 'text',
				'default_value' => 'Plan (contacts)',
				'wrapper'       => ['width' => '50'],
			],
			[
				'key'           => 'field_hp_spt_header_price',
				'label'         => 'Header: Price column',
				'name'          => 'header_price',
				'type'          => 'text',
				'default_value' => 'Annual price',
				'wrapper'       => ['width' => '50'],
			],
			[
				'key'          => 'field_hp_spt_rows',
				'label'        => 'Rows',
				'name'         => 'rows',
				'type'         => 'repeater',
				'layout'       => 'block',
				'button_label' => 'Add row',
				'min'          => 1,
				'sub_fields'   => [
					[
						'key'     => 'field_hp_spt_row_plan_label',
						'label'   => 'Plan label',
						'name'    => 'plan_label',
						'type'    => 'text',
						'wrapper' => ['width' => '25'],
					],
					[
						'key'     => 'field_hp_spt_row_price',
						'label'   => 'Price',
						'name'    => 'price',
						'type'    => 'text',
						'wrapper' => ['width' => '20'],
					],
					[
						'key'     => 'field_hp_spt_row_sub_text',
						'label'   => 'Sub-text (small grey)',
						'name'    => 'sub_text',
						'type'    => 'text',
						'wrapper' => ['width' => '25'],
					],
					[
						'key'           => 'field_hp_spt_row_button_text',
						'label'         => 'Button text',
						'name'          => 'button_text',
						'type'          => 'text',
						'default_value' => 'Get Started',
						'wrapper'       => ['width' => '15'],
					],
					[
						'key'     => 'field_hp_spt_row_button_url',
						'label'   => 'Button URL (Stripe checkout)',
						'name'    => 'button_url',
						'type'    => 'url',
						'wrapper' => ['width' => '15'],
					],
				],
			],
		],
	]);
});
