import React from 'react';
import { __ } from '@wordpress/i18n';
import { env } from '../api';

/**
 * Is this a licensed install?
 *
 * `enforced` is false whenever no licensing provider is configured — a
 * development build, or a self-hosted copy. In that case nothing is Pro and
 * no upsell should appear; inventing a paywall where none is enforced would
 * be a lie about the product the user actually has.
 */
export function isPro() {
	const l = env.license || {};
	return !l.enforced || !!l.licensed;
}

/**
 * Is this the Pro BUILD, regardless of licence state?
 *
 * A customer who installed the Pro ZIP but has not activated the licence yet
 * is not a prospect: showing them a sales pitch reads as being charged twice.
 * Every upsell surface must branch on this, and say "activate" instead.
 */
export function isProBuild() {
	return 'pro' === env.edition;
}

/**
 * Where an upsell button goes.
 *
 * Every one of these used to resolve to `manageUrl`, which is Freemius'
 * Account screen — a page the SDK does not register until the customer has
 * opted in. On a fresh free install WordPress answered that slug by sending
 * the visitor to the dashboard, so pressing "Upgrade to Pro" appeared to do
 * nothing at all. The backend now returns null for a page that does not
 * exist, and this picks the first destination that is real.
 */
export function licenseUrl() {
	const l = env.license || {};
	// An unlicensed Pro build goes straight to Freemius activation — the
	// account page would ask them to connect first anyway, with more steps.
	if (isProBuild() && l.enforced && !l.licensed && l.activateUrl) {
		return l.activateUrl;
	}
	// The free edition sells; the licensed one manages.
	if (!isProBuild() && l.upgradeUrl) {
		return l.upgradeUrl;
	}
	return l.manageUrl || '#/settings/license';
}

/**
 * What stands in a Pro feature's place in the free edition.
 *
 * Not a locked door with no handle: it names the feature, says in one line
 * what it does for you, and links to the licence screen. A gate that only
 * says "Pro" reads as a bug, which is how the first gated build felt.
 */
export function ProNotice({ title, children, compact = false }) {
	return (
		<div className={`ars-pro${compact ? ' is-compact' : ''}`}>
			<span className="ars-pro-badge">{__('Pro', 'ai-rank-suite')}</span>
			<div className="min-w-0">
				<div className="ars-pro-title">{title}</div>
				{children && <p className="ars-pro-text">{children}</p>}
			</div>
			<a className="ars-btn ars-btn-ghost ars-pro-cta" href={licenseUrl()}>
				{isProBuild() ? __('Activate license', 'ai-rank-suite') : __('See Pro', 'ai-rank-suite')}
			</a>
		</div>
	);
}

/**
 * Renders `children` on a licensed install, the notice otherwise.
 *
 * The free edition ships without the Pro modules entirely, so their screens
 * are never mounted at all — this is for the mixed screens, where one card of
 * an otherwise-free page is the paid part.
 */
export default function Pro({ title, note, children, compact = false }) {
	if (isPro()) return children;
	return (
		<ProNotice title={title} compact={compact}>
			{note}
		</ProNotice>
	);
}
