<?php
/**
 * Custom Post Type Handler
 *
 * @package PCChangelogManager
 */

// Prevent direct access to the file.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles registration and management of the changelog custom post type.
 */
class PC_CLM_Post_Type {

	/**
	 * Post type slug.
	 *
	 * @var string
	 */
	const POST_TYPE_SLUG = 'pc_changelog';

	/**
	 * Constructor.
	 */
	public function __construct() {
		// Intentionally empty.
	}

	/**
	 * Register the custom post type.
	 */
	public function register() {
		$labels = array(
			'name'                  => _x( 'Changelogs', 'Post type general name', 'pc-changelog-manager-abc123' ),
			'singular_name'         => _x( 'Changelog Entry', 'Post type singular name', 'pc-changelog-manager-abc123' ),
			'menu_name'             => _x( 'Changelogs', 'Admin Menu text', 'pc-changelog-manager-abc123' ),
			'name_admin_bar'        => _x( 'Changelog Entry', 'Add New on Toolbar', 'pc-changelog-manager-abc123' ),
			'add_new'               => __( 'Add New', 'pc-changelog-manager-abc123' ),
			'add_new_item'          => __( 'Add New Changelog Entry', 'pc-changelog-manager-abc123' ),
			'new_item'              => __( 'New Changelog Entry', 'pc-changelog-manager-abc123' ),
			'edit_item'             => __( 'Edit Changelog Entry', 'pc-changelog-manager-abc123' ),
			'view_item'             => __( 'View Changelog Entry', 'pc-changelog-manager-abc123' ),
			'all_items'             => __( 'All Changelog Entries', 'pc-changelog-manager-abc123' ),
			'search_items'          => __( 'Search Changelog Entries', 'pc-changelog-manager-abc123' ),
			'parent_item_colon'     => __( 'Parent Changelog Entries:', 'pc-changelog-manager-abc123' ),
			'not_found'             => __( 'No changelog entries found.', 'pc-changelog-manager-abc123' ),
			'not_found_in_trash'    => __( 'No changelog entries found in Trash.', 'pc-changelog-manager-abc123' ),
			'featured_image'        => _x( 'Changelog Cover Image', 'Overrides the "Featured Image" phrase for this post type.', 'pc-changelog-manager-abc123' ),
			'set_featured_image'    => _x( 'Set cover image', 'Overrides the "Set featured image" phrase.', 'pc-changelog-manager-abc123' ),
			'remove_featured_image' => _x( 'Remove cover image', 'Overrides the "Remove featured image" phrase.', 'pc-changelog-manager-abc123' ),
			'use_featured_image'    => _x( 'Use as cover image', 'Overrides the "Use as featured image" phrase.', 'pc-changelog-manager-abc123' ),
		);

		$args = array(
			'labels'             => $labels,
			'public'             => true,
			'publicly_queryable' => true,
			'show_ui'            => true,
			'show_in_menu'       => false,
			'query_var'          => true,
			'rewrite'            => array( 'slug' => 'changelog-entry' ),
			'capability_type'    => 'post',
			'map_meta_cap'       => true,
			'has_archive'        => false,
			'hierarchical'       => false,
			'menu_position'      => 30,
			'menu_icon'          => 'dashicons-backup',
			'supports'           => array(
				'title',
				'editor',
				'revisions',
				'author',
			),
			'show_in_rest'       => true,
			'rest_base'          => 'changelog',
		);

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

	/**
	 * Get the post type slug.
	 *
	 * @return string
	 */
	public function get_post_type_slug() {
		return self::POST_TYPE_SLUG;
	}

	/**
	 * Get changelog entries.
	 *
	 * @param array $args Query arguments.
	 * @return WP_Query
	 */
	public function get_entries( $args = array() ) {
		$defaults = array(
			'post_type'      => self::POST_TYPE_SLUG,
			'post_status'    => 'publish',
			'posts_per_page' => -1,
			'orderby'        => 'date',
			'order'          => 'DESC',
		);

		$args = wp_parse_args( $args, $defaults );

		return new WP_Query( $args );
	}

	/**
	 * Get a single changelog entry.
	 *
	 * @param int $post_id Post ID.
	 * @return WP_Post|null
	 */
	public function get_entry( $post_id ) {
		$post = get_post( $post_id );

		if ( ! $post || self::POST_TYPE_SLUG !== $post->post_type ) {
			return null;
		}

		return $post;
	}

	/**
	 * Get categories for changelog entries.
	 *
	 * @return array
	 */
	public function get_categories() {
		return array(
			'new-features'   => __( 'New Features', 'pc-changelog-manager-abc123' ),
			'bug-fixes'      => __( 'Bug Fixes', 'pc-changelog-manager-abc123' ),
			'improvements'   => __( 'Improvements', 'pc-changelog-manager-abc123' ),
			'security'       => __( 'Security', 'pc-changelog-manager-abc123' ),
			'deprecated'     => __( 'Deprecated', 'pc-changelog-manager-abc123' ),
			'removed'        => __( 'Removed', 'pc-changelog-manager-abc123' ),
		);
	}

	/**
	 * Get changelog entry meta.
	 *
	 * @param int    $post_id Post ID.
	 * @param string $key     Meta key.
	 * @param bool   $single  Whether to return a single value.
	 * @return mixed
	 */
	public function get_meta( $post_id, $key, $single = true ) {
		return get_post_meta( $post_id, '_pc_clm_' . $key, $single );
	}

	/**
	 * Set changelog entry meta.
	 *
	 * @param int    $post_id Post ID.
	 * @param string $key     Meta key.
	 * @param mixed  $value   Meta value.
	 * @return bool
	 */
	public function update_meta( $post_id, $key, $value ) {
		return update_post_meta( $post_id, '_pc_clm_' . $key, $value );
	}
}
