<?php
/**
 * Admin: License Page.
 *
 * @package The Studio Player
 */

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

/**
 * Build runtime state for the license page.
 *
 * @param bool $has_pro_assets Whether Pro assets are active.
 * @return array
 */
function thespl_get_license_page_runtime_state( $has_pro_assets ) {
	$state = array(
		'license_status'  => array(),
		'license_state'   => 'free',
		'license_offline' => false,
		'account_issue'   => false,
		'fs'              => null,
		'portal_url'      => thespl_get_freemius_account_url(),
		'unique_affix'    => '',
	);

	return apply_filters( 'thespl_license_page_runtime_state', $state, (bool) $has_pro_assets );
}

/**
 * Render license page.
 *
 * @return void
 */
function thespl_render_license_page() {
	if ( ! thespl_current_user_can_policy( 'license_view' ) ) {
		return;
	}

	$can_manage_license     = thespl_current_user_can_policy( 'license_manage' );
	$has_pro_assets         = function_exists( 'thespl_has_pro_assets' ) ? (bool) thespl_has_pro_assets() : false;
	$customer_dashboard_url = thespl_get_customer_portal_url();
	$license_runtime        = thespl_get_license_page_runtime_state( $has_pro_assets );
	$license_status         = isset( $license_runtime['license_status'] ) && is_array( $license_runtime['license_status'] ) ? $license_runtime['license_status'] : array();
	$license_state          = isset( $license_runtime['license_state'] ) ? (string) $license_runtime['license_state'] : 'free';
	$license_offline        = ! empty( $license_runtime['license_offline'] );
	$account_issue          = ! empty( $license_runtime['account_issue'] );
	$portal_url             = isset( $license_runtime['portal_url'] ) ? (string) $license_runtime['portal_url'] : thespl_get_freemius_account_url();
	$unique_affix           = isset( $license_runtime['unique_affix'] ) ? (string) $license_runtime['unique_affix'] : '';
	$pricing_url            = thespl_get_marketing_pricing_url();
	$ver                    = thespl_get_readme_stable_tag_version();
	$plugin_name            = __( 'The Studio Player', 'the-studio-player' );
	$page_label             = __( 'License', 'the-studio-player' );
	$page_title             = $plugin_name;
	if ( '' !== $ver ) {
		$page_title .= ' - ' . $ver;
		if ( $has_pro_assets ) {
			$page_title .= ' Pro';
		}
	}
	$page_title .= ' - ' . $page_label;
	?>
	<div class="wrap thespl-admin-wrapper">
		<h1><?php echo esc_html( $page_title ); ?></h1>

		<?php thespl_render_remote_admin_banners(); ?>

		<?php thespl_render_admin_top_nav( 'thespl-license' ); ?>

		<div class="section-box">
			<span class="section-title"><?php esc_html_e( 'License & Account', 'the-studio-player' ); ?></span>
			<div class="thespl-admin-grid">
				<div class="thespl-admin-card">
					<h2><?php esc_html_e( 'Pro Features', 'the-studio-player' ); ?></h2>
					<ul class="thespl-feature-chips">
						<li class="thespl-feature-chips__item">
							<span class="thespl-feature-chips__label">
								<?php esc_html_e( 'Compare up to 4 mixes / variants', 'the-studio-player' ); ?>
							</span>
							<span class="thespl-feature-chip <?php echo $has_pro_assets ? 'thespl-feature-chip--on' : 'thespl-feature-chip--off'; ?>">
								<?php
								echo $has_pro_assets ? esc_html__( 'Included', 'the-studio-player' ) : esc_html__( 'Available in Pro', 'the-studio-player' );
								?>
							</span>
						</li>
						<li class="thespl-feature-chips__item">
							<span class="thespl-feature-chips__label">
								<?php esc_html_e( '48 kHz / 24-bit lossless audio', 'the-studio-player' ); ?>
							</span>
							<span class="thespl-feature-chip <?php echo $has_pro_assets ? 'thespl-feature-chip--on' : 'thespl-feature-chip--off'; ?>">
								<?php
								echo $has_pro_assets ? esc_html__( 'Included', 'the-studio-player' ) : esc_html__( 'Available in Pro', 'the-studio-player' );
								?>
							</span>
						</li>
						<li class="thespl-feature-chips__item">
							<span class="thespl-feature-chips__label">
								<?php esc_html_e( 'Waveform JSON generation (new uploads)', 'the-studio-player' ); ?>
							</span>
							<span class="thespl-feature-chip <?php echo $has_pro_assets ? 'thespl-feature-chip--on' : 'thespl-feature-chip--off'; ?>">
								<?php
								echo $has_pro_assets ? esc_html__( 'Included', 'the-studio-player' ) : esc_html__( 'Available in Pro', 'the-studio-player' );
								?>
							</span>
						</li>
						<li class="thespl-feature-chips__item">
							<span class="thespl-feature-chips__label">
								<?php esc_html_e( 'Waveform rebuilder tool (for existing audio)', 'the-studio-player' ); ?>
							</span>
							<span class="thespl-feature-chip <?php echo $has_pro_assets ? 'thespl-feature-chip--on' : 'thespl-feature-chip--off'; ?>">
								<?php
								echo $has_pro_assets ? esc_html__( 'Included', 'the-studio-player' ) : esc_html__( 'Available in Pro', 'the-studio-player' );
								?>
							</span>
						</li>
						<li class="thespl-feature-chips__item">
							<span class="thespl-feature-chips__label">
								<?php esc_html_e( 'Recovery tool (re-index Media Library)', 'the-studio-player' ); ?>
							</span>
							<span class="thespl-feature-chip <?php echo $has_pro_assets ? 'thespl-feature-chip--on' : 'thespl-feature-chip--off'; ?>">
								<?php
								echo $has_pro_assets ? esc_html__( 'Included', 'the-studio-player' ) : esc_html__( 'Available in Pro', 'the-studio-player' );
								?>
							</span>
						</li>
						<li class="thespl-feature-chips__item">
							<span class="thespl-feature-chips__label">
								<?php esc_html_e( 'Priority support (< 24h)', 'the-studio-player' ); ?>
							</span>
							<span class="thespl-feature-chip <?php echo $has_pro_assets ? 'thespl-feature-chip--on' : 'thespl-feature-chip--off'; ?>">
								<?php
								echo $has_pro_assets ? esc_html__( 'Included', 'the-studio-player' ) : esc_html__( 'Available in Pro', 'the-studio-player' );
								?>
							</span>
						</li>
					</ul>
				</div>
				<div class="thespl-admin-card">
					<h2><?php esc_html_e( 'License & Account', 'the-studio-player' ); ?></h2>
					<div class="thespl-admin-card__content">
						<?php
						$badge_label        = '';
						$status_text        = '';
						$cta_label          = '';
						$cta_url            = '';
						$cta_is_overlay     = false;
						$cta_requires_admin = false;
						$tone_class         = 'thespl-license-status--neutral';

						if ( ! $has_pro_assets ) {
							$badge_label = __( 'Free', 'the-studio-player' );
							$status_text = __( 'You are using the Free edition. Install the Pro assets to unlock advanced features.', 'the-studio-player' );
							$cta_label   = __( 'Get Pro', 'the-studio-player' );
							$cta_url     = $pricing_url;
							$tone_class  = 'thespl-license-status--neutral';
						} elseif ( $license_offline ) {
							$badge_label = __( 'Status', 'the-studio-player' );
							$status_text = __( 'License status is currently unavailable. Please check your connection.', 'the-studio-player' );
							$cta_label   = '';
							$cta_url     = '';
							$tone_class  = 'thespl-license-status--neutral';
						} elseif ( 'pro_active' === $license_state ) {
							$badge_label        = __( 'Pro', 'the-studio-player' );
							$status_text        = __( 'License active on this site.', 'the-studio-player' );
							$cta_label          = __( 'Manage your License', 'the-studio-player' );
							$cta_url            = $portal_url;
							$cta_requires_admin = true;
							$tone_class         = 'thespl-license-status--positive';
						} elseif ( $account_issue ) {
							$badge_label        = __( 'Attention', 'the-studio-player' );
							$status_text        = __( 'Your Pro license needs attention. Please review your account.', 'the-studio-player' );
							$cta_label          = __( 'Open account', 'the-studio-player' );
							$cta_url            = $portal_url;
							$cta_requires_admin = true;
							$tone_class         = 'thespl-license-status--warning';
						} else {
							$badge_label        = __( 'Pro', 'the-studio-player' );
							$status_text        = __( 'Pro assets are present but no active license was detected.', 'the-studio-player' );
							$cta_label          = __( 'Activate license', 'the-studio-player' );
							$cta_url            = '#';
							$cta_is_overlay     = ( '' !== $unique_affix );
							$cta_requires_admin = true;
							$tone_class         = 'thespl-license-status--warning';
						}
						if ( $has_pro_assets ) {
							$license_overrides = apply_filters(
								'thespl_license_card_overrides',
								array(
									'enabled'     => false,
									'badge_label' => $badge_label,
									'status_text' => $status_text,
									'cta_label'   => $cta_label,
									'cta_url'     => $cta_url,
									'tone_class'  => $tone_class,
								),
								$license_state,
								$license_status,
								$portal_url
							);
							if ( isset( $license_overrides['enabled'] ) && true === (bool) $license_overrides['enabled'] ) {
								$badge_label        = isset( $license_overrides['badge_label'] ) ? (string) $license_overrides['badge_label'] : $badge_label;
								$status_text        = isset( $license_overrides['status_text'] ) ? (string) $license_overrides['status_text'] : $status_text;
								$cta_label          = isset( $license_overrides['cta_label'] ) ? (string) $license_overrides['cta_label'] : $cta_label;
								$cta_url            = isset( $license_overrides['cta_url'] ) ? (string) $license_overrides['cta_url'] : $cta_url;
								$cta_requires_admin = true;
								$tone_class         = isset( $license_overrides['tone_class'] ) ? (string) $license_overrides['tone_class'] : $tone_class;
							}
						}
						?>
						<div class="thespl-license-status <?php echo esc_attr( $tone_class ); ?>">
							<span class="thespl-license-status__badge"><?php echo esc_html( $badge_label ); ?></span>
							<span class="thespl-license-status__text"><?php echo esc_html( $status_text ); ?></span>
							<?php if ( $cta_label && ( $cta_url || $cta_is_overlay ) ) : ?>
								<?php if ( $cta_requires_admin && ! $can_manage_license ) : ?>
									<span class="thespl-license-status__cta" aria-disabled="true" title="<?php echo esc_attr__( 'To gain access, contact your administrator.', 'the-studio-player' ); ?>"><?php echo esc_html( '🔒 ' . __( 'Admin only', 'the-studio-player' ) ); ?></span>
								<?php else : ?>
									<?php if ( $cta_is_overlay && '' !== $unique_affix ) : ?>
										<span class="activate-license <?php echo esc_attr( $unique_affix ); ?>">
											<a href="#" class="button button-primary thespl-license-status__cta"><?php echo esc_html( $cta_label ); ?></a>
										</span>
									<?php else : ?>
										<a href="<?php echo esc_url( $cta_url ); ?>" class="thespl-license-status__cta" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $cta_label ); ?></a>
									<?php endif; ?>
								<?php endif; ?>
							<?php endif; ?>
						</div>
						<?php if ( ! $has_pro_assets ) : ?>
							<p class="thespl-admin-text">
								<?php esc_html_e( 'Already have Pro? Download and activate the Pro plugin assets from your customer dashboard.', 'the-studio-player' ); ?>
								<a href="<?php echo esc_url( $customer_dashboard_url ); ?>" target="_blank" rel="noopener noreferrer">
									<?php esc_html_e( 'Open your customer dashboard', 'the-studio-player' ); ?>
								</a>
							</p>
						<?php endif; ?>
						<?php if ( ! $has_pro_assets ) : ?>
							<p class="thespl-admin-text">
								<?php esc_html_e( 'Unlock Pro to compare up to 4 mixes, enable high-res audio and restore waveforms for existing tracks.', 'the-studio-player' ); ?>
							</p>
						<?php endif; ?>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php
}
