<?php

namespace Rtc\PlantShopWC\Admin\Views\Partials;

/**
 * ProCta: Static helper for rendering Pro upsell CTAs and building Freemius URLs.
 */
class ProCta {
    /**
     * Build the Freemius upgrade URL with UTM parameters for analytics.
     *
     * @param string $feature_key Machine-friendly feature identifier, used for tracking.
     * @param array  $args        Optional args: period, is_trial, utm (array), extra (array).
     * @return string URL
     */
    public static function freemius_upgrade_url( string $feature_key = 'general', array $args = array() ): string {
        try {
            $fs = \plantshop_wc_fs();
        } catch ( \Throwable $e ) {
            // Fallback to pricing page if FS not ready.
            $base = 'https://www.plantplugin.com/en/#preise';
            return esc_url_raw(
                add_query_arg(
                    array(
                        'utm_source'   => 'plugin',
                        'utm_medium'   => 'cta',
                        'utm_campaign' => 'upsell',
                        'utm_content'  => sanitize_key( $feature_key ),
                    ),
                    $base
                )
            );
        }

        $period   = $args['period'] ?? ( defined( 'WP_FS__PERIOD_ANNUALLY' ) ? WP_FS__PERIOD_ANNUALLY : null );
        $is_trial = (bool) ( $args['is_trial'] ?? false );

        $url = null;
        try {
            if ( $is_trial && method_exists( $fs, 'get_upgrade_url_with_trial' ) ) {
                $url = $fs->get_upgrade_url_with_trial();
            } elseif ( $period !== null ) {
                $url = $fs->get_upgrade_url( $period );
            } else {
                $url = $fs->get_upgrade_url();
            }
        } catch ( \Throwable $e ) {
            $url = $fs->get_upgrade_url();
        }

        $utm = $args['utm'] ?? array();
        $utm = array_merge(
            array(
                'utm_source'   => 'plugin',
                'utm_medium'   => 'cta',
                'utm_campaign' => 'upsell',
                'utm_content'  => sanitize_key( $feature_key ),
            ),
            is_array( $utm ) ? $utm : array()
        );

        $extra = $args['extra'] ?? array();
        $qs    = array_merge( $utm, is_array( $extra ) ? $extra : array() );

        return esc_url_raw( add_query_arg( $qs, $url ) );
    }

    /**
     * Render a reusable CTA block to upsell Pro features.
     * Uses DaisyUI + TailwindCSS classes already configured by the plugin.
     *
     * @param array $args
     *  - variant: inline|card|banner (default card)
     *  - feature_key: string for tracking (default 'general')
     *  - title: string (localized preferred)
     *  - description: string
     *  - bullets: array of strings
     *  - button_label: string (default __('Get Pro','plantshop-wc'))
     *  - button_url: string (defaults to Freemius upgrade URL)
     *  - secondary_label: string
     *  - secondary_url: string
     *  - dismissible: bool (not persisted here; caller can handle state)
     *  - icon: optional emoji or HTML
     */
    public static function render( array $args = array() ): void {
        $variant         = $args['variant'] ?? 'card';
        $feature_key     = is_string( $args['feature_key'] ?? '' ) ? $args['feature_key'] : 'general';
        $title           = $args['title'] ?? __( 'Unlock Pro features', 'plantshop-wc' );
        $description     = $args['description'] ?? __( 'Upgrade to PlantShop for WooCommerce Pro to enable this feature.', 'plantshop-wc' );
        $bullets         = is_array( $args['bullets'] ?? null ) ? $args['bullets'] : array();
        $button_label    = $args['button_label'] ?? __( 'Get Pro', 'plantshop-wc' );
        $button_url      = $args['button_url'] ?? self::freemius_upgrade_url( $feature_key );
        $secondary_label = $args['secondary_label'] ?? '';
        $secondary_url   = $args['secondary_url'] ?? '';
        $icon            = $args['icon'] ?? '🔒';
        // New optional cosmetics (non-breaking)
        $badge    = $args['badge'] ?? __( 'Pro', 'plantshop-wc' );
        $footnote = $args['footnote'] ?? '';

        $button_label = is_string( $button_label ) ? $button_label : __( 'Get Pro', 'plantshop-wc' );
        $title        = is_string( $title ) ? $title : '';
        $description  = is_string( $description ) ? $description : '';

        $button_url    = esc_url( $button_url );
        $secondary_url = $secondary_url ? esc_url( $secondary_url ) : '';
        $has_secondary = $secondary_label && $secondary_url;
        $has_badge     = is_string( $badge ) && $badge !== '';
        $has_footnote  = is_string( $footnote ) && $footnote !== '';

        // Variants output different minimal wrappers but share DaisyUI visual language.
        switch ( $variant ) {
            case 'banner':
                echo '<div class="plantshop-cta alert bg-base-200 border border-base-content/20 rounded-box my-4">
                        <span class="text-lg">' . esc_html( $title ) . '</span>
                        <div class="ml-auto flex gap-2 items-center">
                          <a class="btn btn-primary btn-sm" href="' . esc_url( $button_url ) . '">' . esc_html( $button_label ) . '</a>';
                if ( $has_secondary ) {
                    echo '<a class="btn btn-ghost btn-sm" href="' . esc_url( $secondary_url ) . '">' . esc_html( $secondary_label ) . '</a>';
                }
                echo '</div>
                      </div>';
                return;

            case 'inline':
                echo '<span class="plantshop-cta inline-flex items-center gap-2 bg-base-200 text-base-content/80 px-3 py-2 rounded-btn">
                        <span class="text-xl" aria-hidden="true">' . wp_kses_post( $icon ) . '</span>
                        <span>' . esc_html( $description ? $description : $title ) . '</span>
                        <a class="btn btn-link color-primary no-underline hover:no-underline px-2" href="' . esc_url( $button_url ) . '">' . esc_html( $button_label ) . '</a>';
                if ( $has_secondary ) {
                    echo '<a class="btn btn-link no-underline hover:no-underline px-2" href="' . esc_url( $secondary_url ) . '">' . esc_html( $secondary_label ) . '</a>';
                }
                echo '</span>';
                return;

            case 'card':
            default:
                // Fancy card with soft gradient background, Pro badge, checkmark bullets, and gradient button
                echo '<div class="plantshop-cta card bg-base-100 border border-base-content/15 shadow-sm my-6 rounded-box relative overflow-hidden">
                        <div class="absolute inset-0 pointer-events-none -z-[1] rounded-box bg-gradient-to-tr from-primary/10 via-accent/5 to-transparent [mask-image:radial-gradient(120%_80%_at_0%_0%,_black,_transparent)]"></div>
                        <div class="card-body">
                          <div class="flex items-start gap-4">
                            <div class="shrink-0 size-12 rounded-full ring-1 ring-base-content/10 bg-gradient-to-tr from-warning/15 to-primary/10 flex items-center justify-center text-2xl" aria-hidden="true">' . wp_kses_post( $icon ) . '</div>
                            <div class="flex-1 min-w-0">
                              <div class="flex items-start gap-3">
                                <h3 class="card-title text-xl leading-tight flex-1">' . esc_html( $title ) . '</h3>';
                if ( $has_badge ) {
                    echo '<span class="badge badge-primary badge-outline">' . esc_html( $badge ) . '</span>';
                }
                echo '            </div>';
                if ( $description ) {
                    echo '<p class="opacity-80 mt-2">' . esc_html( $description ) . '</p>';
                }
                if ( ! empty( $bullets ) ) {
                    echo '<ul class="mt-4 space-y-2">';
                    foreach ( $bullets as $b ) {
                        echo '<li class="flex items-start gap-2"><svg class="size-5 text-success mt-[2px]" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6L9 17l-5-5"/></svg><span>' . esc_html( (string) $b ) . '</span></li>';
                    }
                    echo '</ul>';
                }
                echo '      <div class="card-actions mt-6">
                                <a class="btn btn-primary" href="' . esc_url( $button_url ) . '">' . esc_html( $button_label ) . '</a>';
                if ( $has_secondary ) {
                    echo '<a class="btn btn-ghost" href="' . esc_url( $secondary_url ) . '">' . esc_html( $secondary_label ) . '</a>';
                }
                echo '      </div>';
                if ( $has_footnote ) {
                    echo '<p class="mt-3 text-sm opacity-70">' . esc_html( $footnote ) . '</p>';
                }
                echo '            </div>
                          </div>
                        </div>
                      </div>';
                return;
        }
    }
}
