<?php
/**
 * Registers template library CPT and taxonomy.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Template_Post_Type {
	public function register_hooks() {
		add_action( 'init', array( $this, 'register' ) );
	}

	public function register() {
		register_post_type(
			Niwa_AI_Template_Library_Types::CPT_TEMPLATE,
			array(
				'labels'              => array(
					'name'          => __( 'Niwa Templates', 'niwa-ai' ),
					'singular_name' => __( 'Niwa Template', 'niwa-ai' ),
				),
				'public'              => false,
				'publicly_queryable'  => false,
				'show_ui'             => false,
				'show_in_menu'        => false,
				'show_in_rest'        => false,
				'supports'            => array( 'title', 'author' ),
				'capability_type'     => 'post',
				'map_meta_cap'        => true,
				'exclude_from_search' => true,
				'rewrite'             => false,
			)
		);

		register_taxonomy(
			Niwa_AI_Template_Library_Types::TAXONOMY,
			array( Niwa_AI_Template_Library_Types::CPT_TEMPLATE, Niwa_AI_Template_Library_Types::CPT_SNIPPET ),
			array(
				'labels'            => array(
					'name'          => __( 'Niwa Template Types', 'niwa-ai' ),
					'singular_name' => __( 'Niwa Template Type', 'niwa-ai' ),
				),
				'public'            => false,
				'show_ui'           => false,
				'show_admin_column' => false,
				'show_in_rest'      => false,
				'hierarchical'      => false,
				'rewrite'           => false,
			)
		);

		$this->ensure_terms();
	}

	private function ensure_terms() {
		foreach ( Niwa_AI_Template_Library_Types::labels() as $slug => $label ) {
			if ( ! term_exists( $slug, Niwa_AI_Template_Library_Types::TAXONOMY ) ) {
				wp_insert_term( $label, Niwa_AI_Template_Library_Types::TAXONOMY, array( 'slug' => $slug ) );
			}
		}
	}
}
