<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Shared configuration: which post types the builder handles, and who may use it.
 *
 * Every answer here is a FILTER, not a constant: a site can narrow or widen the builder's reach
 * without touching the plugin, and that is the difference between an integration and a takeover.
 */
class AIPB_Config {

	/**
	 * Post types the builder MAY handle. Whether it actually does is decided content by content
	 * by {@see AIPB_Ownership}: this only says where Aura is allowed.
	 *
	 * It covers EVERY post type with a UI (post, page and any custom post type), so the Studio can
	 * open them all, plus synced patterns (wp_block) and the legacy section CPT. WordPress's
	 * INTERNAL types — the ones that are not "content" (media, menu items, FSE templates and so
	 * on) — are left out.
	 *
	 * Filter with 'aipb_builder_post_types' to narrow or widen the list (for instance to exclude
	 * WooCommerce products, or to keep to posts and pages).
	 */
	public static function builder_post_types() {
		$types = get_post_types( array( 'show_ui' => true ), 'names' );

		// WordPress internals: they make no sense as builder content. wp_template and
		// wp_template_part are NOT excluded — the Studio handles them as components (see
		// component_post_types()). Only the genuinely internal types stay out.
		$types = array_diff( $types, array(
			'attachment', 'nav_menu_item', 'revision', 'custom_css', 'customize_changeset',
			'oembed_cache', 'user_request', 'wp_navigation',
			'wp_global_styles', 'wp_font_family', 'wp_font_face',
		) );

		// Guarantees the types the Studio handles even if some registration hid them from the UI
		// (FSE templates come with show_ui = false).
		$types = array_merge( array_values( $types ), self::component_post_types(), self::template_post_types() );

		return apply_filters( 'aipb_builder_post_types', array_values( array_unique( $types ) ) );
	}

	public static function is_builder_post_type( $post_type ) {
		return in_array( $post_type, self::builder_post_types(), true );
	}

	/**
	 * The capability required to use the builder.
	 *
	 * Templates hold raw HTML/CSS/JS, executable <script> included: they belong to whoever is
	 * already trusted to write unfiltered markup. Default 'unfiltered_html' (admin/editor on a
	 * single site; super admin on multisite).
	 */
	public static function required_cap() {
		return apply_filters( 'aipb_required_cap', 'unfiltered_html' );
	}

	public static function user_can_build() {
		return current_user_can( self::required_cap() );
	}

	/**
	 * "Component/pattern" types: ONE reusable section, not a stack of blocks.
	 *  - wp_block: WordPress's synced pattern (the preferred one from now on)
	 *  - ai_section: the legacy CPT, kept for compatibility.
	 *
	 * FSE templates are NOT here: they hold a set of sections, so they are page-like (a stack of
	 * blocks) just like pages — see template_post_types() and is_page_like().
	 */
	public static function component_post_types() {
		return apply_filters( 'aipb_component_post_types', array( 'wp_block', AIPB_CPT::POST_TYPE ) );
	}

	/** The FSE templates alone (a subset of the components), which need materialising. */
	public static function template_post_types() {
		return array( 'wp_template', 'wp_template_part' );
	}

	public static function is_template_type( $post_type ) {
		return in_array( $post_type, self::template_post_types(), true );
	}

	/**
	 * Types the Studio can open in Gutenberg's place: patterns (a single section) and FSE
	 * templates (a stack of sections). Used by the redirect and by the ways out, which apply to
	 * both — but only on the individual entities Aura owns ({@see AIPB_Ownership}).
	 */
	public static function studio_native_types() {
		return array_merge( self::component_post_types(), self::template_post_types() );
	}

	public static function is_studio_native( $post_type ) {
		return in_array( $post_type, self::studio_native_types(), true );
	}

	public static function is_component_type( $post_type ) {
		return in_array( $post_type, self::component_post_types(), true );
	}

	/** A "stack of blocks" content (page/post): builder, but not a component. */
	public static function is_page_like( $post_type ) {
		return self::is_builder_post_type( $post_type ) && ! self::is_component_type( $post_type );
	}
}
