<?php
/**
 * Setup class for article availabilities.
 *
 * @package flo-sync
 */

namespace NMFS\Sync\Items\Article_Availabilities;

use NMFS\Shared;
use WC_Product;

/**
 * Class Setup
 */
class Setup {

	public const CPT_KEY = 'nmf_article_avail';

	/**
	 * Sets up article availability.
	 *
	 * @return void
	 */
	public static function setup(): void {
		add_action( 'init', array( __CLASS__, 'register_cpt' ) );

		$enabled = Article_Availabilities::is_enabled();

		if ( apply_filters( 'nmf_check_product_availability', $enabled ) ) {
			add_filter( 'woocommerce_is_purchasable', array( __CLASS__, 'is_product_purchasable' ), 10, 2 );
		}
	}

	/**
	 * Registers a custom post type for Article Availability.
	 *
	 * @return void
	 */
	public static function register_cpt(): void {

		$labels = array(
			'name'               => _x( 'Article Availability', 'Post type general name', 'flo-sync' ),
			'singular_name'      => _x( 'Article Availability', 'Post type singular name', 'flo-sync' ),
			'menu_name'          => _x( 'Article Availabilities', 'Admin Menu text', 'flo-sync' ),
			'name_admin_bar'     => _x( 'Article Availability', 'Add New on Toolbar', 'flo-sync' ),
			'add_new'            => __( 'Add New', 'flo-sync' ),
			'add_new_item'       => __( 'Add New Article Availability', 'flo-sync' ),
			'new_item'           => __( 'New Article Availability', 'flo-sync' ),
			'edit_item'          => __( 'Edit Article Availability', 'flo-sync' ),
			'view_item'          => __( 'View Article Availability', 'flo-sync' ),
			'all_items'          => __( 'All Article Availabilities', 'flo-sync' ),
			'search_items'       => __( 'Search Article Availabilities', 'flo-sync' ),
			'parent_item_colon'  => __( 'Parent Article Availabilities:', 'flo-sync' ),
			'not_found'          => __( 'No Article Availabilities found.', 'flo-sync' ),
			'not_found_in_trash' => __( 'No Article Availabilities found in Trash.', 'flo-sync' ),
		);

		$args = array(
			'labels'             => $labels,
			'public'             => false,
			'publicly_queryable' => true,
			'show_ui'            => Shared::instance()->debug->is_on(),
			'show_in_menu'       => Shared::instance()->debug->is_on(),
			'query_var'          => true,
			'rewrite'            => false,
			'has_archive'        => false,
			'hierarchical'       => false,
			'supports'           => array( 'title', 'custom-fields' ),
		);

		register_post_type( self::CPT_KEY, $args );
	}

	/**
	 * Determines if a product is purchasable based on article availability.
	 *
	 * @param bool       $is_purchasable Whether the product is purchasable.
	 * @param WC_Product $product        The product to check article availability for.
	 *
	 * @return bool True if the product is available, false otherwise
	 * @noinspection PhpUnusedParameterInspection -- Filter parameter.
	 */
	public static function is_product_purchasable( bool $is_purchasable, WC_Product $product ): bool {
		return Article_Availabilities::is_article_available( $product->get_id() );
	}
}
