<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

include_once SFTCY_WORDFORM_PLUGIN_DIR . 'admin/views/pages-header-template.php';

$features_dir           = trailingslashit( SFTCY_WORDFORM_PLUGIN_DIR ) . 'features/';
$basic_features_file    = $features_dir . 'basic-features.json';
$pro_features_file      = $features_dir . 'pro-features.json';
$comparison_ui_file     = $features_dir . 'feature-comparison-ui.json';
$basic_features_payload = file_exists( $basic_features_file ) ? json_decode( file_get_contents( $basic_features_file ), true ) : array();
$pro_features_payload   = file_exists( $pro_features_file ) ? json_decode( file_get_contents( $pro_features_file ), true ) : array();
$comparison_ui_payload  = file_exists( $comparison_ui_file ) ? json_decode( file_get_contents( $comparison_ui_file ), true ) : array();

$upgrade_url = class_exists( 'SftcyWordformFeatures' ) ? SftcyWordformFeatures::wordform_get_upgrade_url() : 'https://softcoy.com/products/wordform-pro/checkout';
$hero_items  = array();
$table_rows  = array();
$notice_cards = array();
$cta_copy     = array();

if ( ! empty( $comparison_ui_payload['uiSections'] ) && is_array( $comparison_ui_payload['uiSections'] ) ) {
	foreach ( $comparison_ui_payload['uiSections'] as $section ) {
		$section_key = isset( $section['key'] ) ? sanitize_key( $section['key'] ) : '';
		if ( 'hero' === $section_key ) {
			$hero_items = ! empty( $section['items'] ) && is_array( $section['items'] ) ? $section['items'] : array();
		} elseif ( 'comparison_table' === $section_key ) {
			$table_rows = ! empty( $section['rows'] ) && is_array( $section['rows'] ) ? $section['rows'] : array();
		} elseif ( 'limited_features_cards' === $section_key ) {
			$notice_cards = ! empty( $section['items'] ) && is_array( $section['items'] ) ? $section['items'] : array();
		} elseif ( 'cta' === $section_key ) {
			$cta_copy = ! empty( $section['copy'] ) && is_array( $section['copy'] ) ? $section['copy'] : array();
		}
	}
}

$feature_labels = array();
foreach ( array( $basic_features_payload, $pro_features_payload ) as $payload ) {
	foreach ( array( 'modules', 'elements' ) as $section_key ) {
		if ( empty( $payload[ $section_key ] ) || ! is_array( $payload[ $section_key ] ) ) {
			continue;
		}

		foreach ( $payload[ $section_key ] as $module ) {
			if ( ! empty( $module['features'] ) && is_array( $module['features'] ) ) {
				foreach ( $module['features'] as $feature ) {
					if ( ! empty( $feature['key'] ) && ! empty( $feature['label'] ) ) {
						$feature_labels[ sanitize_key( $feature['key'] ) ] = sanitize_text_field( $feature['label'] );
					}
				}
			}

			if ( ! empty( $module['key'] ) && ! empty( $module['label'] ) ) {
				$feature_labels[ sanitize_key( $module['key'] ) ] = sanitize_text_field( $module['label'] );
			}
		}
	}
}

$feature_labels['google_calendar']          = 'Google Calendar';
$feature_labels['google_sheets']            = 'Google Sheets';
$feature_labels['custom_webhook']           = 'Custom Webhook';
$feature_labels['payment-coupon']           = 'Payment Coupon';
$feature_labels['submit-button']            = 'Submit Button Styling';
$feature_labels['payment_reporting_dashboard'] = 'Payment Reporting Dashboard';
$feature_labels['connection_send_logs']     = 'Connection Send Logs';

$feature_flag_registry = class_exists( 'SftcyWordformFeatures' ) ? SftcyWordformFeatures::wordform_get_feature_flags_registry() : array( 'features' => array() );
$feature_flag_map      = ! empty( $feature_flag_registry['features'] ) && is_array( $feature_flag_registry['features'] ) ? $feature_flag_registry['features'] : array();

$resolve_feature_status = static function( $feature_key, $tier ) use ( $feature_flag_map ) {
	$feature_key = sanitize_key( str_replace( '-', '_', (string) $feature_key ) );
	$tier        = sanitize_key( (string) $tier );

	if ( empty( $feature_flag_map[ $feature_key ] ) || ! is_array( $feature_flag_map[ $feature_key ] ) ) {
		return array(
			'status' => 'basic' === $tier ? 'pro_only' : 'full',
			'limits' => array(),
		);
	}

	$feature_map = $feature_flag_map[ $feature_key ];
	return array(
		'status' => isset( $feature_map[ $tier ] ) ? sanitize_key( $feature_map[ $tier ] ) : ( 'basic' === $tier ? 'pro_only' : 'full' ),
		'limits' => ! empty( $feature_map['limits'][ $tier ] ) && is_array( $feature_map['limits'][ $tier ] ) ? $feature_map['limits'][ $tier ] : array(),
	);
};

$render_status_badge = static function( $status, $limits = array() ) {
	$status = sanitize_key( (string) $status );
	if ( 'full' === $status ) {
		return '<span class="wordform-go-pro-status-badge is-full">' . esc_html__( 'Included', 'wordform' ) . '</span>';
	}
	if ( 'limited' === $status ) {
		$label = ! empty( $limits['label'] ) ? sanitize_text_field( $limits['label'] ) : __( 'Included with limits', 'wordform' );
		return '<span class="wordform-go-pro-status-badge is-limited">' . esc_html( $label ) . '</span>';
	}

	return '<span class="wordform-go-pro-status-badge is-pro">' . esc_html__( 'Pro only', 'wordform' ) . '</span>';
};
?>

<div class="wrap wordform-go-pro-page" style="margin:1%;">
	<style>
		.wordform-go-pro-hero{display:grid;grid-template-columns:1.4fr 1fr;gap:24px;padding:28px;border-radius:20px;background:linear-gradient(135deg,#f7fbff 0%,#eef6ff 55%,#fdf7fb 100%);border:1px solid #d9e7fb}
		.wordform-go-pro-hero h1{margin:0 0 12px;font-size:32px;line-height:1.15;color:#11325f}
		.wordform-go-pro-hero p{margin:0;color:#4a5d77;font-size:15px;max-width:720px}
		.wordform-go-pro-actions{display:flex;gap:12px;flex-wrap:wrap;margin-top:22px}
		.wordform-go-pro-actions .button{min-height:42px;padding:0 16px}
		.wordform-go-pro-summary{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:16px}
		.wordform-go-pro-summary-card,.wordform-go-pro-card,.wordform-go-pro-cta,.wordform-go-pro-table{background:#fff;border:1px solid #e4e9f2;border-radius:18px;box-shadow:0 8px 24px rgba(15,23,42,.04)}
		.wordform-go-pro-summary-card{padding:18px}
		.wordform-go-pro-summary-card strong{display:block;color:#0f2f5f;font-size:15px;margin-bottom:8px}
		.wordform-go-pro-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:18px;margin-top:22px}
		.wordform-go-pro-card{padding:20px}
		.wordform-go-pro-card h3,.wordform-go-pro-table h2,.wordform-go-pro-limits h2,.wordform-go-pro-cta h2{margin:0 0 10px;color:#153766}
		.wordform-go-pro-card p,.wordform-go-pro-limits p,.wordform-go-pro-cta p{color:#53657f}
		.wordform-go-pro-table{margin-top:24px;padding:20px}
		.wordform-go-pro-table table{width:100%;border-collapse:collapse}
		.wordform-go-pro-table th,.wordform-go-pro-table td{padding:12px 10px;border-bottom:1px solid #edf1f6;text-align:left;vertical-align:top}
		.wordform-go-pro-table th{color:#26456f}
		.wordform-go-pro-table tr:last-child td{border-bottom:none}
		.wordform-go-pro-status-badge{display:inline-flex;align-items:center;padding:6px 10px;border-radius:999px;font-size:12px;font-weight:600}
		.wordform-go-pro-status-badge.is-full{background:#e6f7ef;color:#0d7a43}
		.wordform-go-pro-status-badge.is-limited{background:#fff7e5;color:#996500}
		.wordform-go-pro-status-badge.is-pro{background:#eef2ff;color:#3557d5}
		.wordform-go-pro-group-row td{background:#f8fbff;font-weight:600;color:#163864}
		.wordform-go-pro-limits{margin-top:24px}
		.wordform-go-pro-limits-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:18px}
		.wordform-go-pro-cta{margin-top:24px;padding:24px;display:flex;justify-content:space-between;align-items:center;gap:20px}
		@media (max-width: 1100px){.wordform-go-pro-hero,.wordform-go-pro-summary,.wordform-go-pro-grid,.wordform-go-pro-limits-grid,.wordform-go-pro-cta{grid-template-columns:1fr;display:grid}.wordform-go-pro-cta{display:block}}
	</style>

	<div class="wordform-go-pro-hero">
		<div>
			<h1><?php esc_html_e( 'Upgrade WordForm to Pro', 'wordform' ); ?></h1>
			<p><?php esc_html_e( 'WordForm already gives you a strong free builder. Pro unlocks premium fields, more integrations, richer payment flows, and deeper diagnostics so you can run production-ready booking, survey, checkout, and workflow forms from one builder.', 'wordform' ); ?></p>
			<div class="wordform-go-pro-actions">
				<a class="button button-primary button-large" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php echo esc_html( ! empty( $cta_copy['primaryButtonLabel'] ) ? $cta_copy['primaryButtonLabel'] : __( 'Buy WordForm Pro', 'wordform' ) ); ?></a>
				<a class="button button-secondary button-large" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php echo esc_html( ! empty( $cta_copy['secondaryButtonLabel'] ) ? $cta_copy['secondaryButtonLabel'] : __( 'Compare Features', 'wordform' ) ); ?></a>
			</div>
		</div>
		<div class="wordform-go-pro-summary">
			<div class="wordform-go-pro-summary-card">
				<strong><?php esc_html_e( 'More Apps', 'wordform' ); ?></strong>
				<span><?php esc_html_e( 'Unlock Google Calendar, Zapier, Slack, Make, CRM tools, and marketing sync.', 'wordform' ); ?></span>
			</div>
			<div class="wordform-go-pro-summary-card">
				<strong><?php esc_html_e( 'Premium Fields', 'wordform' ); ?></strong>
				<span><?php esc_html_e( 'Use signature, matrix survey, rich text, and more advanced business-ready inputs.', 'wordform' ); ?></span>
			</div>
			<div class="wordform-go-pro-summary-card">
				<strong><?php esc_html_e( 'Deeper Checkout', 'wordform' ); ?></strong>
				<span><?php esc_html_e( 'Add more gateways, coupons, payment reports, and stronger operational visibility.', 'wordform' ); ?></span>
			</div>
		</div>
	</div>

	<?php if ( ! empty( $hero_items ) ) { ?>
	<div class="wordform-go-pro-grid">
		<?php foreach ( $hero_items as $hero_item ) { ?>
		<div class="wordform-go-pro-card">
			<h3><?php echo esc_html( isset( $hero_item['title'] ) ? $hero_item['title'] : '' ); ?></h3>
			<p><?php echo esc_html( isset( $hero_item['description'] ) ? $hero_item['description'] : '' ); ?></p>
		</div>
		<?php } ?>
	</div>
	<?php } ?>

	<div class="wordform-go-pro-table">
		<h2><?php esc_html_e( 'Basic vs Pro Comparison', 'wordform' ); ?></h2>
		<table>
			<thead>
				<tr>
					<th><?php esc_html_e( 'Feature', 'wordform' ); ?></th>
					<th><?php esc_html_e( 'WordForm Free', 'wordform' ); ?></th>
					<th><?php esc_html_e( 'WordForm Pro', 'wordform' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $table_rows as $row_group ) { ?>
				<tr class="wordform-go-pro-group-row">
					<td colspan="3"><?php echo esc_html( isset( $row_group['group'] ) ? $row_group['group'] : '' ); ?></td>
				</tr>
					<?php foreach ( (array) $row_group['items'] as $feature_key ) {
						$basic_status = $resolve_feature_status( $feature_key, 'basic' );
						$pro_status   = $resolve_feature_status( $feature_key, 'pro' );
						$feature_label = isset( $feature_labels[ sanitize_key( $feature_key ) ] ) ? $feature_labels[ sanitize_key( $feature_key ) ] : ucwords( str_replace( array( '-', '_' ), ' ', (string) $feature_key ) );
					?>
				<tr>
					<td><?php echo esc_html( $feature_label ); ?></td>
					<td><?php echo $render_status_badge( $basic_status['status'], $basic_status['limits'] ); ?></td>
					<td><?php echo $render_status_badge( $pro_status['status'], $pro_status['limits'] ); ?></td>
				</tr>
					<?php } ?>
				<?php } ?>
			</tbody>
		</table>
	</div>

	<?php if ( ! empty( $notice_cards ) ) { ?>
	<div class="wordform-go-pro-limits">
		<h2><?php esc_html_e( 'Included in Free with Limits', 'wordform' ); ?></h2>
		<div class="wordform-go-pro-limits-grid">
			<?php foreach ( $notice_cards as $card ) { ?>
			<div class="wordform-go-pro-card">
				<h3><?php echo esc_html( isset( $card['title'] ) ? $card['title'] : '' ); ?></h3>
				<p><?php echo esc_html( isset( $card['description'] ) ? $card['description'] : '' ); ?></p>
			</div>
			<?php } ?>
		</div>
	</div>
	<?php } ?>

	<div class="wordform-go-pro-cta">
		<div>
			<h2><?php echo esc_html( ! empty( $cta_copy['title'] ) ? $cta_copy['title'] : __( 'Unlock WordForm Pro', 'wordform' ) ); ?></h2>
			<p><?php echo esc_html( ! empty( $cta_copy['description'] ) ? $cta_copy['description'] : __( 'Get premium fields, advanced integrations, deeper checkout tools, and business-ready reporting.', 'wordform' ) ); ?></p>
		</div>
		<div class="wordform-go-pro-actions">
			<a class="button button-primary button-large" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php echo esc_html( ! empty( $cta_copy['primaryButtonLabel'] ) ? $cta_copy['primaryButtonLabel'] : __( 'Buy WordForm Pro', 'wordform' ) ); ?></a>
		</div>
	</div>
</div>
