<?php

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

if ( ! function_exists( 'discountify_bogo_doc' ) ) {
	function discountify_bogo_doc( $selected_type = '' ) {
		$docs = array(
			'bogo_free'        => array(
				'desc'    => esc_html__( 'Buy one product, get the same product completely free.', 'discountify' ),
				'example' => esc_html__( 'Example: Buy 1 shirt, get a 2nd shirt FREE', 'discountify' ),
			),
			'bogo_percent'     => array(
				'desc'    => esc_html__( 'Buy one product, get the same product at a percentage discount.', 'discountify' ),
				'example' => esc_html__( 'Example: Buy 1 mug, get the 2nd at 50% off', 'discountify' ),
			),
			'buy_n_get_one'    => array(
				'desc'    => esc_html__( 'Buy N quantity of a product, get 1 free of the same product.', 'discountify' ),
				'example' => esc_html__( 'Example: Buy any 2 bags, get the 3rd FREE', 'discountify' ),
			),
			'buy_x_get_y'      => array(
				'desc'    => esc_html__( 'Buy product X, get a different product Y free or discounted.', 'discountify' ),
				'example' => esc_html__( 'Example: Buy this phone case, get a screen protector FREE', 'discountify' ),
			),
			'category_bogo'    => array(
				'desc'    => esc_html__( 'Buy from a category, get a specific item free or discounted.', 'discountify' ),
				'example' => esc_html__( 'Example: Buy any shoe, get a pair of socks FREE', 'discountify' ),
			),
			'bogo_fixed_price' => array(
				'desc'    => esc_html__( 'Buy one product, get the same product at a fixed price.', 'discountify' ),
				'example' => esc_html__( 'Example: Buy 1 at $40, get the 2nd for only $10', 'discountify' ),
			),
		);

		// If no type selected, default to first type for new rules
		if ( empty( $selected_type ) ) {
			$selected_type = 'bogo_free';
		}

		$wrapper_class = 'bogo_doc_wrapper';
		$html          = "<div class='{$wrapper_class}'>";
		foreach ( $docs as $type => $doc ) {
			$active_class = ( $type === $selected_type ) ? '' : ' d-none';
			$html        .= "<div class='{$type}_doc{$active_class}'>";
			$html        .= "<div>{$doc['desc']}</div>";
			$html        .= "<div>{$doc['example']}</div>";
			$html        .= '</div>';
		}
		$html .= '</div>';

		return $html;
	}
}

	// Backward compatibility: map old buy_x_get_x to bogo_free
	$bogo_type_map = array(
		'buy_x_get_x' => 'bogo_free',
	);
	if ( isset( $bogo_type_map[ $bogo_discount_type ] ) ) {
		$bogo_discount_type = $bogo_type_map[ $bogo_discount_type ];
	}

	// BOGO Discount Type dropdown - 6 types
	$discountify_bogo_args = array(
		'label'       => esc_html__( 'Bogo Discount Type:', 'discountify' ),
		'id'          => 'bogo_discount_type',
		'select_type' => 'single',
		'selected'    => $bogo_discount_type,
		'doc'         => discountify_bogo_doc( $bogo_discount_type ),
		'options'         => array(
			'bogo_free'        => esc_html__( 'BOGO Free (Buy 1 Get 1 Free)', 'discountify' ),
			'bogo_percent'     => esc_html__( 'BOGO Percent Off', 'discountify' ),
			'buy_n_get_one'    => esc_html__( 'Buy N Get 1 Free', 'discountify' ),
			'buy_x_get_y'      => esc_html__( 'Buy X Get Y (Different Product)', 'discountify' ),
			'category_bogo'    => esc_html__( 'Category BOGO', 'discountify' ),
			'bogo_fixed_price' => esc_html__( 'BOGO Fixed Price', 'discountify' ),
		),
	);
	discountify_select_field( $discountify_bogo_args );

	// Bogo Discount In - shown for buy_x_get_y and category_bogo
	$discountify_bogo_x_y_cond = in_array( $bogo_discount_type, array( 'buy_x_get_y', 'category_bogo' ), true ) ? '' : 'd-none';
	$discountify_bogo_args     = array(
		'label'           => esc_html__( 'Bogo Discount In:', 'discountify' ),
		'id'              => 'bogo_discount_in',
		'condition_class' => 'bogo_discount_in_block ' . $discountify_bogo_x_y_cond,
		'select_type'     => 'single',
		'selected'        => $bogo_discount_in,
		'options'         => array(
			'product'  => esc_html__( 'Buy X get Y - Product', 'discountify' ),
			'category' => esc_html__( 'Buy X get Y - Category', 'discountify' ),
		),
	);
	discountify_select_field( $discountify_bogo_args );

	?>
<?php
