<?php
/**
 * Free vs Pro: layouts, image caps, upgrade helpers.
 *
 * @package Image_Gallery_Custom_Post_Type
 */

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

/**
 * Feature limits for the hosted plugin (Pro unlocks via add-on or filter).
 */
class NZP_IGL_Limits {

	public const LAYOUT_FREE = array( 'grid', 'listing' );

	public const LAYOUT_PRO = array( 'mosaic', 'showcase', 'carousel' );

	/**
	 * Default donation URL (PayPal). Shipped so **Donate** works without a child theme or external site.
	 * Override or clear with filter `nzp_igl_donate_url`.
	 */
	private const DONATE_URL_DEFAULT = 'https://www.paypal.com/ncp/payment/VYMD7S9Q22JXE';

	/**
	 * Whether advanced (paid) features are available.
	 *
	 * Pro add-ons should use `add_filter( 'nzp_igl_is_pro', '__return_true', 5 );` — no constants required.
	 *
	 * @return bool
	 */
	public static function is_pro() {
		return (bool) apply_filters( 'nzp_igl_is_pro', false );
	}

	/**
	 * Max images stored and shown per gallery (free tier is lower).
	 *
	 * @return int
	 */
	public static function max_gallery_images() {
		$max = self::is_pro() ? 100 : 20;
		return (int) apply_filters( 'nzp_igl_max_gallery_images', $max );
	}

	/**
	 * All layout slugs (free + Pro).
	 *
	 * @return string[]
	 */
	public static function all_layout_slugs() {
		return array_merge( self::LAYOUT_FREE, self::LAYOUT_PRO );
	}

	/**
	 * @param string $layout Layout slug.
	 * @return bool
	 */
	public static function can_use_layout( $layout ) {
		$layout = sanitize_key( (string) $layout );
		if ( in_array( $layout, self::LAYOUT_FREE, true ) ) {
			return true;
		}
		if ( in_array( $layout, self::LAYOUT_PRO, true ) ) {
			return self::is_pro();
		}
		return false;
	}

	/**
	 * Downgrade Pro-only layouts when not licensed.
	 *
	 * @param string $layout Layout slug.
	 * @return string
	 */
	public static function normalize_layout( $layout ) {
		$layout = sanitize_key( (string) $layout );
		$all    = self::all_layout_slugs();
		if ( $layout === '' || ! in_array( $layout, $all, true ) ) {
			return 'grid';
		}
		return self::can_use_layout( $layout ) ? $layout : 'grid';
	}

	/**
	 * Optional external URL (advanced). Pro is enabled by activating the Pro add-on plugin — this filter is not used by default UI.
	 *
	 * @return string
	 */
	public static function upgrade_url() {
		return (string) apply_filters( 'nzp_igl_upgrade_url', '' );
	}

	/**
	 * Donation URL. When non-empty, a Donate control appears in admin.
	 *
	 * Defaults to {@see self::DONATE_URL_DEFAULT}. Use `nzp_igl_donate_url` to replace the link or return '' to hide **Donate**.
	 *
	 * @return string
	 */
	public static function donate_url() {
		return (string) apply_filters( 'nzp_igl_donate_url', self::DONATE_URL_DEFAULT );
	}

	/**
	 * Apply tier cap and optional shortcode `limit` (1–100 or 0 = use tier max).
	 *
	 * @param int[] $ids         Ordered attachment IDs.
	 * @param int   $limit_attr  Shortcode `limit` attribute.
	 * @return int[]
	 */
	public static function slice_image_ids( array $ids, $limit_attr ) {
		$limit_attr = max( 0, min( 100, absint( $limit_attr ) ) );
		$cap        = self::max_gallery_images();
		$n          = count( $ids );
		if ( $limit_attr > 0 ) {
			$take = min( $limit_attr, $cap, $n );
		} else {
			$take = min( $cap, $n );
		}
		return array_slice( $ids, 0, $take );
	}
}
