<?php
/** Native WordPress license settings page. */

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

/**
 * Macht den notwendigen nächsten Schritt direkt in der Plugin-Liste sichtbar.
 *
 * @param array<int,string> $links Vorhandene Aktionslinks.
 * @return array<int,string>
 */
function wpagent_companion_plugin_action_links( array $links ): array {
	$control_link = sprintf(
		'<a href="%1$s">%2$s</a>',
		esc_url( admin_url( 'admin.php?page=wpagently' ) ),
		esc_html__( 'Kontrollzentrum', 'wp-agent-companion' )
	);
	$license_link = sprintf(
		'<a href="%1$s">%2$s</a>',
		esc_url( admin_url( 'tools.php?page=wpagently-license' ) ),
		esc_html__( 'Free / Lizenz', 'wp-agent-companion' )
	);
	array_unshift( $links, $control_link, $license_link );
	return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( WPAGENT_COMPANION_DIR . 'wp-agent-companion.php' ), 'wpagent_companion_plugin_action_links' );

function wpagent_companion_license_admin_menu(): void {
	add_management_page(
		__( 'wpagently-Lizenz', 'wp-agent-companion' ),
		__( 'wpagently-Lizenz', 'wp-agent-companion' ),
		'manage_options',
		'wpagently-license',
		'wpagent_companion_license_admin_page'
	);
}
add_action( 'admin_menu', 'wpagent_companion_license_admin_menu' );

function wpagent_companion_license_admin_action(): void {
	if ( ! current_user_can( 'manage_options' ) || empty( $_POST['wpagent_license_action'] ) ) {
		return;
	}
	check_admin_referer( 'wpagent_license_update' );

	$action = sanitize_key( wp_unslash( $_POST['wpagent_license_action'] ) );
	if ( 'activate' === $action ) {
		$key    = isset( $_POST['wpagent_license_key'] ) ? sanitize_text_field( wp_unslash( $_POST['wpagent_license_key'] ) ) : '';
		$result = wpagent_companion_license_activate( $key );
	} elseif ( 'refresh' === $action ) {
		$result = wpagent_companion_license_refresh();
	} elseif ( 'deactivate' === $action ) {
		$result = wpagent_companion_license_deactivate();
	} else {
		$result = new WP_Error( 'wpagent_license_unknown_action', __( 'Unbekannte Lizenzaktion.', 'wp-agent-companion' ) );
	}

	$kind    = is_wp_error( $result ) ? 'error' : 'success';
	$message = is_wp_error( $result )
		? $result->get_error_message()
		: __( 'Die wpagently-Lizenz wurde aktualisiert.', 'wp-agent-companion' );
	set_transient( 'wpagent_license_notice_' . get_current_user_id(), array( $kind, $message ), 60 );

	wp_safe_redirect( admin_url( 'tools.php?page=wpagently-license' ) );
	exit;
}
add_action( 'admin_init', 'wpagent_companion_license_admin_action' );

function wpagent_companion_license_admin_page(): void {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$notice = get_transient( 'wpagent_license_notice_' . get_current_user_id() );
	if ( $notice ) {
		delete_transient( 'wpagent_license_notice_' . get_current_user_id() );
		printf( '<div class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>', esc_attr( $notice[0] ), esc_html( $notice[1] ) );
	}

	$status         = wpagent_companion_license_public_status();
	$private_state  = wpagent_companion_license_state();
	$has_activation = ! empty( $private_state['key'] ) && ! empty( $private_state['activation_id'] );
	?>
	<div class="wrap wpagently-admin">
		<div class="wpagently-admin__head">
			<h1><?php esc_html_e( 'wpagently-Lizenz', 'wp-agent-companion' ); ?></h1>
			<?php if ( $status['licensed'] ) : ?>
				<p><?php esc_html_e( 'Die Aktivierung zählt diese WordPress-Website gegen das Site-Limit deiner Polar-Lizenz.', 'wp-agent-companion' ); ?></p>
			<?php else : ?>
				<p><?php esc_html_e( 'Free ist aktiv. Du kannst 34 Abilities ohne Aufrufslimit und ohne Lizenzschlüssel verwenden.', 'wp-agent-companion' ); ?></p>
			<?php endif; ?>
		</div>
		<div class="wpagently-card wpagently-card--tight">
		<table class="widefat striped">
			<tbody>
				<tr><th><?php esc_html_e( 'Zugang', 'wp-agent-companion' ); ?></th><td><span class="wpagently-badge wpagently-badge--safe"><?php echo esc_html( $status['licensed'] ? __( 'Bezahlt', 'wp-agent-companion' ) : __( 'Free aktiv', 'wp-agent-companion' ) ); ?></span></td></tr>
				<?php if ( $has_activation && ! $status['licensed'] ) : ?>
					<tr><th><?php esc_html_e( 'Bezahlte Lizenz', 'wp-agent-companion' ); ?></th><td><?php echo esc_html( $status['paid_license_status'] ); ?></td></tr>
				<?php endif; ?>
				<tr><th><?php esc_html_e( 'Zuletzt geprüft', 'wp-agent-companion' ); ?></th><td><?php echo esc_html( $status['checked_at'] ?? __( 'Noch nie', 'wp-agent-companion' ) ); ?></td></tr>
				<tr><th><?php esc_html_e( 'Gültig bis', 'wp-agent-companion' ); ?></th><td><?php echo esc_html( $status['expires_at'] ?? __( 'Nicht angegeben', 'wp-agent-companion' ) ); ?></td></tr>
				<tr><th><?php esc_html_e( 'Site-Limit', 'wp-agent-companion' ); ?></th><td><?php echo esc_html( $status['limit_activations'] ?? __( 'Nicht angegeben', 'wp-agent-companion' ) ); ?></td></tr>
			</tbody>
		</table>
		</div>

		<form class="wpagently-form--tight" method="post">
			<?php wp_nonce_field( 'wpagent_license_update' ); ?>
			<?php if ( $has_activation ) : ?>
				<button class="button button-primary" name="wpagent_license_action" value="refresh"><?php esc_html_e( 'Lizenz jetzt prüfen', 'wp-agent-companion' ); ?></button>
				<button class="button" name="wpagent_license_action" value="deactivate"><?php esc_html_e( 'Website deaktivieren', 'wp-agent-companion' ); ?></button>
			<?php else : ?>
				<label for="wpagent-license-key"><strong><?php esc_html_e( 'Polar-Lizenzschlüssel', 'wp-agent-companion' ); ?></strong></label>
				<input id="wpagent-license-key" class="regular-text wpagently-key-input" type="password" name="wpagent_license_key" autocomplete="off" required>
				<button class="button button-primary" name="wpagent_license_action" value="activate"><?php esc_html_e( 'Lizenz aktivieren', 'wp-agent-companion' ); ?></button>
			<?php endif; ?>
		</form>
		<?php if ( ! $status['licensed'] ) : ?>
			<p class="description"><?php esc_html_e( 'Mit einer bezahlten Lizenz erhältst du alle 398 Companion-Abilities, davon 312 im empfohlenen Inhaltsprofil, externe Integrationen, Power, Backup, Restore, CLI und Skills.', 'wp-agent-companion' ); ?></p>
		<?php endif; ?>
	</div>
	<?php
}
