<?php

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

/**
 * Resolve Freemius license state, plans, limits, and feature access.
 */
class Content_Miner_License {
	const PLAN_FREE         = 'free';
	const PLAN_PRO          = 'pro';
	const PLAN_AGENCY       = 'agency';
	const PLAN_ENTERPRISE   = 'enterprise';

	const FEATURE_GOOGLE_EVIDENCE  = 'google_evidence';
	const FEATURE_SEMANTIC_ANALYSIS = 'semantic_analysis';
	const FEATURE_LLM_VERDICTS      = 'llm_verdicts';
	const FEATURE_OUTCOMES          = 'outcomes';
	const FEATURE_DIGEST            = 'digest';
	const FEATURE_RENDERED_VERIFICATION = 'rendered_verification';
	const FEATURE_AI_EXPLAIN        = 'ai_explain';
	const FEATURE_CLOUD_BRIDGE      = 'cloud_bridge';

	/** @var object|null */
	private $freemius;

	/** @var bool */
	private $freemius_configured;

	/** @var bool */
	private $offline_pro_fallback;

	/**
	 * @param object|null $freemius Freemius SDK instance or test double.
	 * @param bool        $freemius_configured Whether real Freemius identifiers are configured.
	 * @param bool        $offline_pro_fallback Whether the local Pro build fallback is available.
	 */
	public function __construct( $freemius = null, $freemius_configured = false, $offline_pro_fallback = false ) {
		$this->freemius             = $freemius;
		$this->freemius_configured  = (bool) $freemius_configured;
		$this->offline_pro_fallback = (bool) $offline_pro_fallback;
	}

	/** Return whether any paid code may run. */
	public function is_pro() {
		if ( $this->freemius_configured ) {
			return is_object( $this->freemius )
				&& method_exists( $this->freemius, 'can_use_premium_code__premium_only' )
				&& (bool) $this->freemius->can_use_premium_code__premium_only();
		}

		return $this->offline_pro_fallback;
	}

	/** Return the active plan slug. */
	public function get_plan() {
		if ( ! $this->is_pro() ) {
			return self::PLAN_FREE;
		}

		if ( ! $this->freemius_configured ) {
			// Offline/local Pro builds preserve the complete pre-Freemius feature set.
			return self::PLAN_ENTERPRISE;
		}

		foreach ( array( self::PLAN_ENTERPRISE, self::PLAN_AGENCY, self::PLAN_PRO ) as $plan ) {
			if ( method_exists( $this->freemius, 'is_plan' ) && $this->freemius->is_plan( $plan, true ) ) {
				return $plan;
			}
		}

		// A valid paid license with an unrecognized plan gets the lowest paid entitlement.
		return self::PLAN_PRO;
	}

	/** Return the configured Freemius plan ID for the current plan. */
	public function get_plan_id() {
		$ids = array(
			self::PLAN_PRO        => defined( 'CONTENT_MINER_FS_PRO_PLAN_ID' ) ? (int) CONTENT_MINER_FS_PRO_PLAN_ID : 0,
			self::PLAN_AGENCY     => defined( 'CONTENT_MINER_FS_AGENCY_PLAN_ID' ) ? (int) CONTENT_MINER_FS_AGENCY_PLAN_ID : 0,
			self::PLAN_ENTERPRISE => defined( 'CONTENT_MINER_FS_ENTERPRISE_PLAN_ID' ) ? (int) CONTENT_MINER_FS_ENTERPRISE_PLAN_ID : 0,
		);

		$plan = $this->get_plan();
		return isset( $ids[ $plan ] ) ? $ids[ $plan ] : 0;
	}

	/** Return whether the active plan includes one premium feature. */
	public function has_feature( $feature ) {
		if ( ! $this->is_pro() ) {
			return false;
		}

		return in_array(
			$feature,
			array(
				self::FEATURE_GOOGLE_EVIDENCE,
				self::FEATURE_SEMANTIC_ANALYSIS,
				self::FEATURE_LLM_VERDICTS,
				self::FEATURE_OUTCOMES,
				self::FEATURE_DIGEST,
				self::FEATURE_RENDERED_VERIFICATION,
				self::FEATURE_AI_EXPLAIN,
				self::FEATURE_CLOUD_BRIDGE,
			),
			true
		);
	}

	/** Return the licensed page ceiling for this installation. */
	public function get_page_limit() {
		$limits = array(
			self::PLAN_FREE       => 500,
			self::PLAN_PRO        => 500,
			self::PLAN_AGENCY     => 50000,
			self::PLAN_ENTERPRISE => PHP_INT_MAX,
		);

		return $limits[ $this->get_plan() ];
	}

	/** Return the licensed site allowance. Freemius enforces activations across separate sites. */
	public function get_site_limit() {
		$limits = array(
			self::PLAN_FREE       => 1,
			self::PLAN_PRO        => 1,
			self::PLAN_AGENCY     => 10,
			self::PLAN_ENTERPRISE => PHP_INT_MAX,
		);

		return $limits[ $this->get_plan() ];
	}

	/** Return a safe Freemius upgrade URL when the SDK is available. */
	public function get_upgrade_url() {
		if ( is_object( $this->freemius ) && method_exists( $this->freemius, 'get_upgrade_url' ) ) {
			return (string) $this->freemius->get_upgrade_url();
		}

		return 'https://www.corristonconsulting.com/products/content-miner#pricing';
	}

	/** Return a UI-ready snapshot without persisting license state. */
	public function get_state( $available_pages = 0 ) {
		$page_limit = $this->get_page_limit();
		return array(
			'configured'     => $this->freemius_configured,
			'is_pro'         => $this->is_pro(),
			'plan'           => $this->get_plan(),
			'plan_id'        => $this->get_plan_id(),
			'page_limit'      => $page_limit,
			'site_limit'      => $this->get_site_limit(),
			'available_pages' => max( 0, (int) $available_pages ),
			'limit_reached'   => PHP_INT_MAX !== $page_limit && (int) $available_pages > $page_limit,
			'upgrade_url'     => $this->get_upgrade_url(),
		);
	}
}
