<?php

if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

class MeprGroupsHelper
{
    /**
     * Get existing products list.
     *
     * @param  MeprGroup $group The group.
     * @return void
     */
    public static function get_existing_products_list($group)
    {
        $products = $group->products();
        if (!empty($products)) {
            foreach ($products as $index => $prd) :
                ?>
        <li class="product-item">
                <?php MeprGroupsHelper::get_products_dropdown($prd->ID); ?>
          <span class="remove-span">
            <a href="" class="remove-product-item" title="Remove Membership">
              <i class="mp-icon mp-icon-cancel-circled mp-16"></i>
            </a>
          </span>
        </li>
                <?php
            endforeach;
        } else {
            ?>
      <li class="product-item">
            <?php MeprGroupsHelper::get_products_dropdown(); ?>
        <span class="remove-span">
          <a href="" class="remove-product-item" title="Remove Membership">
            <i class="mp-icon mp-icon-cancel-circled mp-16"></i>
          </a>
        </span>
      </li>
            <?php
        }
    }

    /**
     * Theme dropdown.
     *
     * @param  string $selected The selected.
     * @return void
     */
    public static function theme_dropdown($selected = null)
    {
        $themes = MeprGroup::group_themes();
        ?>
    <select name="<?php echo esc_attr(MeprGroup::$group_theme_str); ?>" class="group_theme_dropdown">
        <?php
        foreach ($themes as $theme) {
            $css  = basename($theme);
            $name = preg_replace('#\.css$#', '', $css);
            $name = ucwords(preg_replace('#_#', ' ', $name));
            ?>
            <option value="<?php echo esc_attr($css); ?>" <?php selected($css, $selected); ?>>
                <?php echo esc_html($name); ?>
            </option>
            <?php
        }
        ?>
      <option value="custom" <?php selected('custom', $selected); ?>>
        <?php esc_html_e('None / Custom', 'memberpress'); ?>
      </option>
    </select>
        <?php
    }

    /**
     * Get products dropdown.
     *
     * @param  string $chosen The chosen.
     * @return void
     */
    public static function get_products_dropdown($chosen = null)
    {
        $products = MeprCptModel::all('MeprProduct', false, [
            'orderby' => 'title',
            'order'   => 'ASC',
        ]);
        ?>
      <select name="<?php echo esc_attr(MeprGroup::$products_str); ?>[product][]" class="group_products_dropdown">
        <?php foreach ($products as $p) : ?>
          <option value="<?php echo esc_attr($p->ID); ?>" <?php selected($p->ID, $chosen); ?>>
            <?php echo esc_html($p->post_title); ?>
          </option>
        <?php endforeach; ?>
      </select>
        <?php
    }

    /**
     * Get product fallback dropdown.
     *
     * @param  MeprGroup $group The group.
     * @return void
     */
    public static function get_product_fallback_dropdown($group)
    {
        $products = $group->products();
        $selected = $group->fallback_membership;
        ?>
      <select name="<?php echo esc_attr(MeprGroup::$fallback_membership_str); ?>" class="group_theme_dropdown">
        <option value="" <?php selected('', $selected); ?>>
          <?php esc_html_e('Default', 'memberpress'); ?>
        </option>
        <?php foreach ($products as $p) : ?>
          <option value="<?php echo esc_attr($p->ID); ?>" <?php selected($p->ID, $selected); ?>>
            <?php echo esc_html($p->post_title); ?>
          </option>
        <?php endforeach; ?>
      </select>
        <?php
    }

    /**
     * Group page item.
     *
     * @param  MeprProduct $product The product.
     * @param  MeprGroup   $group   The group.
     * @param  boolean     $preview The preview.
     * @return void
     */
    public static function group_page_item($product, $group = null, $preview = false)
    {
        ob_start();
        $benefits = '';

        if ($group === null) {
            $group = new MeprGroup();
        }

        if (!empty($product->pricing_benefits)) {
            $benefits = '<div class="mepr-price-box-benefits-list">';

            foreach ($product->pricing_benefits as $index => $b) {
                if ('' !== trim($b)) {
                    $benefits .= '<div class="mepr-price-box-benefits-item">'
                        . MeprHooks::apply_filters('mepr_price_box_benefit', $b, $index)
                        . '</div>';
                }
            }

            $benefits .= '</div>';
        }

        $user   = MeprUtils::get_currentuserinfo(); // If not logged in, $user will be false.
        $active = true; // Always true for now - that way users can click the button and see the custom "you don't have access" message now.

        $group_classes_str = ($product->is_highlighted) ? 'highlighted' : '';
        $group_classes_str = MeprHooks::apply_filters(
            'mepr_group_css_classes_string',
            $group_classes_str,
            $product,
            $group,
            $preview
        );

        ?>
    <div id="mepr-price-box-<?php echo esc_attr($product->ID); ?>"
      class="mepr-price-box <?php echo esc_attr($group_classes_str); ?>">
        <?php
        $badge_text = !empty($product->pricing_badge_txt)
            ? $product->pricing_badge_txt
            : __('Most Popular', 'memberpress');
        if ($preview) :
            // Always render in preview mode so JS can toggle it.
            ?>
        <div class="mepr-most-popular" <?php echo !$product->is_highlighted ? 'style="display:none;"' : ''; ?>><?php echo esc_html($badge_text); ?></div>
        <?php elseif ($product->is_highlighted) : ?>
        <div class="mepr-most-popular"><?php echo esc_html($badge_text); ?></div>
        <?php endif; ?>
      <div class="mepr-price-box-head">
        <div class="mepr-price-box-title"><?php echo wp_kses_post($product->pricing_title); ?></div>
        <?php if ($preview) : ?>
          <div class="mepr-price-box-price"></div>
          <span class="mepr-price-box-price-loading">
            <img src="<?php echo esc_url(admin_url('/images/wpspin_light.gif')); ?>"/>
          </span>
        <?php elseif ($product->pricing_display !== 'none') : ?>
          <div class="mepr-price-box-price">
            <?php
            $mepr_coupon_code = $product->get_applicable_coupon_code();
            $mepr_coupon_code = $mepr_coupon_code !== '' ? $mepr_coupon_code : null;

            if ($product->pricing_display === 'auto') {
                // When a coupon is applied, show the original price crossed out
                // with the discounted amount below. Full terms are on checkout.
                if (!empty($mepr_coupon_code)) {
                    $original_str   = MeprProductsHelper::format_currency($product, true, null, false);
                    $discount_price = MeprProductsHelper::get_discounted_price($product, $mepr_coupon_code);
                    $discount_str   = MeprAppHelper::format_currency($discount_price);

                    // For recurring memberships, append asterisk and show "first payment" note.
                    $is_recurring = !$product->is_one_time_payment();

                    // Wrap currency symbol in spans for modern template styling.
                    if ($group && !empty($group->modern_template)) {
                        $mepr_options = MeprOptions::fetch();
                        $original_str = preg_replace(
                            '/\/(.*)/',
                            "<span class='mepr-price-box-price-term'>$0</span>",
                            $original_str
                        );
                        $original_str = str_replace(
                            $mepr_options->currency_symbol,
                            '<span class="mepr-price-box-price-currency">' . $mepr_options->currency_symbol . '</span>',
                            $original_str
                        );
                        $discount_str = str_replace(
                            $mepr_options->currency_symbol,
                            '<span class="mepr-price-box-price-currency">' . $mepr_options->currency_symbol . '</span>',
                            $discount_str
                        );
                    }

                    echo '<span class="mepr-price-box-original-price">' . wp_kses_post($original_str) . '</span>';
                    echo '<span class="mepr-price-box-discount-price">' . wp_kses_post($discount_str) . '</span>';
                    $discount_note = $group ? $group->coupon_discount_note : __('first payment', 'memberpress');
                    if ($is_recurring && '' !== $discount_note) {
                        echo '<span class="mepr-price-box-discount-note">'
                            . esc_html($discount_note) . '</span>';
                    }
                } else {
                    $price_str = MeprProductsHelper::format_currency($product, true, null, false);

                    // Wrap currency symbol and billing term in spans for modern template styling.
                    if ($group && !empty($group->modern_template)) {
                        $mepr_options = MeprOptions::fetch();
                        $price_str    = preg_replace(
                            '/\/(.*)/',
                            "<span class='mepr-price-box-price-term'>$0</span>",
                            $price_str
                        );
                        $price_str    = str_replace(
                            $mepr_options->currency_symbol,
                            '<span class="mepr-price-box-price-currency">' . $mepr_options->currency_symbol . '</span>',
                            $price_str
                        );
                    }

                    echo wp_kses_post($price_str);
                }
            } else {
                $custom_str = $product->custom_price;

                // Wrap currency symbol in span for modern template styling.
                if ($group && !empty($group->modern_template)) {
                    $mepr_options   = MeprOptions::fetch();
                    $symbol         = $mepr_options->currency_symbol;
                    $escaped_symbol = esc_html($symbol);
                    $custom_str     = str_replace(
                        $escaped_symbol,
                        '<span class="mepr-price-box-price-currency">' . $escaped_symbol . '</span>',
                        esc_html($custom_str)
                    );
                } else {
                    $custom_str = esc_html($custom_str);
                }

                echo wp_kses_post($custom_str);
                if (!empty($product->custom_price_term)) {
                    echo ' <span class="mepr-price-box-price-term">'
                        . esc_html($product->custom_price_term) . '</span>';
                }
            }
            ?>
          </div>
        <?php endif; ?>
          <?php if ($preview || !empty($product->pricing_heading_txt)) : ?>
          <div class="mepr-price-box-heading"><?php echo wp_kses_post($product->pricing_heading_txt); ?></div>
          <?php endif; ?>
          <?php
            if ($preview || in_array($product->pricing_button_position, ['header','both'], true)) {
                echo wp_kses_post(self::price_box_button($user, $group, $product, $active));
            }
            ?>
      </div>
      <div class="mepr-price-box-benefits"><?php echo wp_kses_post($benefits); ?></div>
      <div class="mepr-price-box-foot">
        <div class="mepr-price-box-footer"><?php echo wp_kses_post($product->pricing_footer_txt); ?></div>
        <?php
        if ($preview || in_array($product->pricing_button_position, ['footer','both'], true)) {
            echo wp_kses_post(self::price_box_button($user, $group, $product, $active));
        }
        ?>
      </div>
    </div>
        <?php
        $output = ob_get_clean();
        echo wp_kses_post(
            MeprHooks::apply_filters('mepr_group_page_item_output', $output, $product, $group, $preview)
        );
    }

    /**
     * Price box button classes.
     *
     * @param  MeprGroup   $grp    The group.
     * @param  MeprProduct $prd    The product.
     * @param  boolean     $active The active.
     * @return string
     */
    public static function price_box_button_classes($grp, $prd, $active)
    {
        $bc = '';

        if ($prd->is_highlighted) {
            $bc .= $grp->page_button_highlighted_class;
        } else {
            $bc .= $grp->page_button_class;
        }

        if (!$active) {
            $bc .= " mepr-disabled {$grp->page_button_disabled_class}";
        }

        return trim($bc);
    }

    /**
     * Price box button.
     *
     * @param  MeprUser    $user    The user.
     * @param  MeprGroup   $group   The group.
     * @param  MeprProduct $product The product.
     * @param  boolean     $active  The active.
     * @return string
     */
    public static function price_box_button($user, $group, $product, $active)
    {
        ob_start();

        ?>
    <div class="mepr-price-box-button">
        <?php
        // All this logic is for showing a "VIEW" button instead of "Buy Now" if the member has already purchased it
        // and the membership access URL is set for that membership - and you can't buy the same membership more than once.
        if (
            $user && !$product->simultaneous_subscriptions &&
            $user->is_already_subscribed_to($product->ID) &&
            !empty($product->access_url)
        ) :
            ?>
          <a href="<?php echo esc_url($product->access_url); ?>"
            class="<?php echo esc_attr(self::price_box_button_classes($group, $product, true)); ?>">
            <?php esc_html_e('View', 'memberpress'); ?>
          </a>
        <?php else : ?>
            <a <?php echo $active ? 'href="' . esc_url($product->url()) . '"' : ''; ?>
              class="<?php echo esc_attr(self::price_box_button_classes($group, $product, $active)); ?>">
                <?php
                echo wp_kses_post(
                    !empty($product->pricing_button_txt) ? $product->pricing_button_txt : __('Sign Up', 'memberpress')
                );
                ?>
            </a>
        <?php endif; ?>
    </div>
        <?php

        return ob_get_clean();
    }

    /**
     * Build the inline CSS custom properties string for a modern template group.
     *
     * @param  MeprGroup $group The group object.
     * @return string The inline style string with CSS custom properties.
     */
    public static function build_modern_style_string($group)
    {
        $opts = is_array($group->modern_template_options) ? $group->modern_template_options : [];

        $radius_map = [
            'none'    => '0',
            'subtle'  => '6px',
            'rounded' => '12px',
        ];
        $shadow_map = [
            'none'      => 'none',
            'subtle'    => '0 1px 3px rgba(0,0,0,0.1)',
            'prominent' => '0 4px 20px rgba(0,0,0,0.12)',
        ];

        $radius = $radius_map[$opts['border_radius'] ?? 'rounded'] ?? '12px';
        $shadow = $shadow_map[$opts['shadow'] ?? 'subtle'] ?? '0 1px 3px rgba(0,0,0,0.1)';

        return sprintf(
            '--mepr-primary:%s;--mepr-accent:%s;--mepr-btn-color:%s;'
            . '--mepr-btn-text-color:%s;--mepr-text-color:%s;--mepr-price-color:%s;'
            . '--mepr-hl-text-color:%s;--mepr-hl-price-color:%s;--mepr-hl-btn-color:%s;'
            . '--mepr-hl-btn-text-color:%s;--mepr-radius:%s;--mepr-shadow:%s;',
            esc_attr($opts['primary_color'] ?? '#2563eb'),
            esc_attr($opts['accent_color'] ?? '#16a34a'),
            esc_attr($opts['button_color'] ?? '#2563eb'),
            esc_attr($opts['btn_text_color'] ?? '#ffffff'),
            esc_attr($opts['text_color'] ?? '#1e293b'),
            esc_attr($opts['price_color'] ?? '#0f172a'),
            esc_attr($opts['hl_text_color'] ?? '#ffffff'),
            esc_attr($opts['hl_price_color'] ?? '#ffffff'),
            esc_attr($opts['hl_btn_color'] ?? '#16a34a'),
            esc_attr($opts['hl_btn_text_color'] ?? '#ffffff'),
            esc_attr($radius),
            esc_attr($shadow)
        );
    }

    /**
     * Get existing coupons list for the group.
     *
     * The public coupon render helpers were moved verbatim into
     * MeprCouponsHelper; these placeholders guard callers of the old
     * location. The class_usable checks keep them strip-safe.
     * mepr:coupon-surface
     *
     * @deprecated 1.13.0 Use MeprCouponsHelper::get_existing_coupons_list() instead.
     *
     * @param  MeprGroup $group The group.
     * @return void
     */
    public static function get_existing_coupons_list($group)
    {
        _deprecated_function(__METHOD__, '1.13.0', 'MeprCouponsHelper::get_existing_coupons_list');

        if (!MeprFeatures::class_usable('MeprCouponsHelper')) {
            return;
        }

        MeprCouponsHelper::get_existing_coupons_list($group);
    }

    /**
     * Get coupons dropdown.
     *
     * @deprecated 1.13.0 Use MeprCouponsHelper::get_coupons_dropdown() instead.
     *
     * @param  integer        $chosen The chosen coupon ID.
     * @param  integer|string $index  The index for the coupon (or 'TEMPLATE_INDEX' for template).
     * @return void
     */
    public static function get_coupons_dropdown($chosen = null, $index = 'TEMPLATE_INDEX')
    {
        _deprecated_function(__METHOD__, '1.13.0', 'MeprCouponsHelper::get_coupons_dropdown');

        if (!MeprFeatures::class_usable('MeprCouponsHelper')) {
            return;
        }

        MeprCouponsHelper::get_coupons_dropdown($chosen, $index);
    }

    /**
     * Get group coupon field map.
     *
     * @deprecated 1.13.0 Use MeprCouponsHelper::get_group_coupon_field_map() instead.
     *
     * @return array<string, string>
     */
    public static function get_group_coupon_field_map(): array
    {
        _deprecated_function(__METHOD__, '1.13.0', 'MeprCouponsHelper::get_group_coupon_field_map');

        if (!MeprFeatures::class_usable('MeprCouponsHelper')) {
            return [];
        }

        return MeprCouponsHelper::get_group_coupon_field_map();
    }
}
