<?php
/**
 * PRO license payload for React admin (2.7).
 *
 * @package Newsroom_AI_Assistant
 */

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

if ( ! function_exists( 'newsai_pro_registry_i18n' ) ) {
	function newsai_pro_registry_i18n() {
		return [
			'loading'       => __( 'Loading PRO license…', 'newsroom-ai-assistant' ),
			'error'         => __( 'PRO license could not load.', 'newsroom-ai-assistant' ),
			'title'         => __( 'PRO License', 'newsroom-ai-assistant' ),
			'subtitle'      => __( 'Activate and manage your Newsroom OS Pro subscription.', 'newsroom-ai-assistant' ),
			'status'        => __( 'Status', 'newsroom-ai-assistant' ),
			'valid'         => __( 'Active', 'newsroom-ai-assistant' ),
			'inactive'      => __( 'Not activated', 'newsroom-ai-assistant' ),
			'expired'       => __( 'Expired', 'newsroom-ai-assistant' ),
			'key'           => __( 'License key', 'newsroom-ai-assistant' ),
			'email'         => __( 'License email', 'newsroom-ai-assistant' ),
			'paid_through'  => __( 'Paid through', 'newsroom-ai-assistant' ),
			'site'          => __( 'Registered site', 'newsroom-ai-assistant' ),
			'activate'      => __( 'Activate license', 'newsroom-ai-assistant' ),
			'refresh'       => __( 'Verify license', 'newsroom-ai-assistant' ),
			'deactivate'    => __( 'Deactivate on this site', 'newsroom-ai-assistant' ),
			'buy'           => __( 'Get PRO at NewsOS.io', 'newsroom-ai-assistant' ),
			'dev_build'     => __( 'Development build — licensing bypassed.', 'newsroom-ai-assistant' ),
			'no_access'     => __( 'Only administrators can manage the license.', 'newsroom-ai-assistant' ),
			'activated'     => __( 'License activated successfully.', 'newsroom-ai-assistant' ),
			'refreshed'     => __( 'License verified.', 'newsroom-ai-assistant' ),
			'deactivated'   => __( 'License deactivated on this site.', 'newsroom-ai-assistant' ),
			'confirm_deact' => __( 'Deactivate Pro on this site?', 'newsroom-ai-assistant' ),
			'key_hint'      => __( 'Format: NROS-XXXX-XXXX-XXXX', 'newsroom-ai-assistant' ),
			'features'      => __( 'PRO features unlocked', 'newsroom-ai-assistant' ),
		];
	}
}

if ( ! function_exists( 'newsai_pro_registry_payload' ) ) {
	function newsai_pro_registry_payload() {
		$pro_build = defined( 'NEWSAI_PRO_BUILD' ) && NEWSAI_PRO_BUILD;
		$lic_valid = function_exists( 'newsai_is_pro' ) && newsai_is_pro();
		$saved_key = function_exists( 'newsai_secret_get' )
			? newsai_secret_get( 'pro_license_key' )
			: (string) get_option( 'newsai_pro_license_key', '' );
		$lic_status  = (string) get_option( 'newsai_pro_license_status', '' );
		$lic_checked = (int) get_option( 'newsai_pro_license_checked_at', 0 );
		$lic_expires = (string) get_option( 'newsai_pro_license_expires_at', '' );
		$lic_email   = (string) get_option( 'newsai_pro_license_email', '' );
		$exp_still_ok = ( $lic_expires === '' || ! function_exists( 'newsai_pro_license_date_still_valid' ) )
			? true
			: newsai_pro_license_date_still_valid( $lic_expires );
		$mask_key = '';
		if ( strlen( $saved_key ) > 10 ) {
			$mask_key = substr( $saved_key, 0, 4 ) . '****' . substr( $saved_key, -4 );
		}

		$features = [];
		if ( $lic_valid ) {
			$features = [
				__( 'Google Search Console sync (Discover, News, Web)', 'newsroom-ai-assistant' ),
				__( 'Chief Insights & AI Daily Brief', 'newsroom-ai-assistant' ),
				__( 'Author Performance & desk coaching', 'newsroom-ai-assistant' ),
				__( 'AI Assist BYOK (headlines, optimize, tags)', 'newsroom-ai-assistant' ),
				__( 'Instant Indexing API & IndexNow', 'newsroom-ai-assistant' ),
			];
		}

		return [
			'canManage'   => current_user_can( 'manage_options' ),
			'isPro'       => $lic_valid,
			'isDevBuild'  => $pro_build,
			'status'      => $lic_status,
			'valid'       => $lic_valid,
			'expired'     => $lic_status === 'valid' && ! $exp_still_ok,
			'maskedKey'   => $mask_key,
			'email'       => $lic_email,
			'expiresAt'   => $lic_expires,
			'checkedAt'   => $lic_checked,
			'siteUrl'     => home_url( '/' ),
			'apiUrl'      => function_exists( 'newsai_pro_license_api_url' ) ? newsai_pro_license_api_url() : '',
			'buyUrl'      => 'https://newsos.io',
			'features'    => $features,
			'licenseNonce'=> wp_create_nonce( 'newsai_pro_license_save' ),
			'i18n'        => newsai_pro_registry_i18n(),
			'urls'        => [
				'classic' => admin_url( 'admin.php?page=newsai-settings&tab=pro&ui=classic' ),
			],
		];
	}
}
