<?php
/**
 * Content schema defaults service
 *
 * Which post types get Article schema, and whether breadcrumb schema is
 * emitted. This is the single state behind the setup wizard's Content step
 * and the Content settings page — both write the same options.
 *
 * @package BW_Schema
 * @since 3.3.0
 */

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

class BW_Schema_Service_Content {

	/**
	 * Option prefix
	 *
	 * @var string
	 */
	const PREFIX = 'bw_schema_';

	/**
	 * Post types that emit Article schema on their singular views
	 *
	 * The team post type is excluded implicitly — team pages always emit
	 * Person schema regardless of this setting.
	 *
	 * @return string[] Post type slugs
	 */
	public static function get_article_post_types() {
		$types = get_option( self::PREFIX . 'article_post_types', null );

		if ( ! is_array( $types ) ) {
			return array( 'post', 'page' ); // Default: match pre-3.3 behavior
		}

		return array_values( array_map( 'sanitize_key', $types ) );
	}

	/**
	 * Set which post types emit Article schema
	 *
	 * @param string[] $post_types Post type slugs
	 * @return void
	 */
	public static function set_article_post_types( $post_types ) {
		$post_types = is_array( $post_types ) ? array_map( 'sanitize_key', $post_types ) : array();
		update_option( self::PREFIX . 'article_post_types', array_values( $post_types ) );
	}

	/**
	 * Whether breadcrumb schema is emitted
	 *
	 * @return bool
	 */
	public static function is_breadcrumbs_enabled() {
		return 'no' !== get_option( self::PREFIX . 'enable_breadcrumbs', 'yes' );
	}

	/**
	 * Enable or disable breadcrumb schema
	 *
	 * @param bool $enabled True to emit breadcrumb schema
	 * @return void
	 */
	public static function set_breadcrumbs_enabled( $enabled ) {
		update_option( self::PREFIX . 'enable_breadcrumbs', $enabled ? 'yes' : 'no' );
	}

	/**
	 * Public post types eligible for Article schema
	 *
	 * Excludes attachments and the team post type (always Person).
	 *
	 * @return array Post type slug => label
	 */
	public static function get_eligible_post_types() {
		$team_post_type = class_exists( 'BW_Schema_Service_People' )
			? BW_Schema_Service_People::get_team_post_type()
			: '';

		$eligible = array();
		foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $post_type ) {
			if ( 'attachment' === $post_type->name || $post_type->name === $team_post_type ) {
				continue;
			}
			$eligible[ $post_type->name ] = $post_type->labels->name;
		}

		return $eligible;
	}
}
