<?php
/**
 * Registers the `flipa_book` custom post type and a Shortcode column in
 * the admin list so authors can copy [flipa id="N"] in one glance.
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

class Flipa_CPT {

	public static function init() {
		add_action( 'init', array( __CLASS__, 'register' ) );
		add_filter( 'manage_' . FLIPA_CPT . '_posts_columns', array( __CLASS__, 'columns' ) );
		add_action( 'manage_' . FLIPA_CPT . '_posts_custom_column', array( __CLASS__, 'column_content' ), 10, 2 );
	}

	public static function register() {
		$labels = array(
			'name'               => __( 'Flipbooks', 'flipa-pdf-flipbook' ),
			'singular_name'      => __( 'Flipbook', 'flipa-pdf-flipbook' ),
			'add_new'            => __( 'Add New', 'flipa-pdf-flipbook' ),
			'add_new_item'       => __( 'Add New Flipbook', 'flipa-pdf-flipbook' ),
			'edit_item'          => __( 'Edit Flipbook', 'flipa-pdf-flipbook' ),
			'new_item'           => __( 'New Flipbook', 'flipa-pdf-flipbook' ),
			'view_item'          => __( 'View Flipbook', 'flipa-pdf-flipbook' ),
			'search_items'       => __( 'Search Flipbooks', 'flipa-pdf-flipbook' ),
			'not_found'          => __( 'No flipbooks yet', 'flipa-pdf-flipbook' ),
			'not_found_in_trash' => __( 'No flipbooks in trash', 'flipa-pdf-flipbook' ),
			'menu_name'          => __( 'Flipa', 'flipa-pdf-flipbook' ),
		);

		register_post_type( FLIPA_CPT, array(
			'labels'       => $labels,
			'public'       => false,      // not a standalone front-end URL; embedded via shortcode/block
			'show_ui'      => true,
			'show_in_menu' => true,
			'show_in_rest' => true,       // enables the block editor + future Gutenberg block
			'menu_icon'    => self::menu_icon(),
			'menu_position'=> 25,
			'supports'     => array( 'title' ),
			'capability_type' => 'post',
			'rewrite'      => false,
		) );
	}

	/**
	 * If the user has dropped a dedicated menu icon into /assets, return its
	 * filename ('menu-icon.svg' preferred, then 'menu-icon.png'); else ''.
	 */
	public static function custom_icon_file() {
		foreach ( array( 'menu-icon.svg', 'menu-icon.png' ) as $f ) {
			if ( file_exists( FLIPA_DIR . 'assets/' . $f ) ) { return $f; }
		}
		return '';
	}

	/**
	 * Side-menu icon. Uses the user's dropped-in menu-icon.(svg|png) if present
	 * (Flipa_Admin::menu_icon_css() masks it so it recolors with the admin
	 * theme). Otherwise falls back to a native WordPress Dashicon.
	 */
	public static function menu_icon() {
		$f = self::custom_icon_file();
		if ( ! $f ) { return 'dashicons-book-alt'; }
		$path = FLIPA_DIR . 'assets/' . $f;
		if ( '.svg' === substr( $f, -4 ) ) {
			return 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( $path ) );
		}
		return FLIPA_URL . 'assets/' . $f; // png → URL
	}

	public static function columns( $cols ) {
		$new = array();
		foreach ( $cols as $k => $v ) {
			$new[ $k ] = $v;
			if ( 'title' === $k ) {
				$new['flipa_shortcode'] = __( 'Shortcode', 'flipa-pdf-flipbook' );
			}
		}
		return $new;
	}

	public static function column_content( $col, $post_id ) {
		if ( 'flipa_shortcode' === $col ) {
			$code = sprintf( '[flipa id="%d"]', $post_id );
			printf(
				'<code class="flipa-sc" style="user-select:all;cursor:text">%s</code>',
				esc_html( $code )
			);
		}
	}
}
