<?php
/**
 * Custom post type used to store brief submissions.
 *
 * @package Briefify
 */

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

/**
 * Class Briefify_CPT
 */
class Briefify_CPT {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'register_post_type' ) );
		add_filter( 'manage_' . BRIEFIFY_CPT . '_posts_columns', array( $this, 'columns' ) );
		add_action( 'manage_' . BRIEFIFY_CPT . '_posts_custom_column', array( $this, 'render_column' ), 10, 2 );
		add_filter( 'post_row_actions', array( $this, 'row_actions' ), 10, 2 );
	}

	/**
	 * Register the briefify_submission custom post type.
	 *
	 * Submissions are private records (not publicly queryable) managed
	 * from the NR Brief Builder admin menu rather than the front end.
	 */
	public function register_post_type() {
		$labels = array(
			'name'               => __( 'Brief submissions', 'nr-brief-builder' ),
			'singular_name'      => __( 'Brief submission', 'nr-brief-builder' ),
			'menu_name'          => __( 'NR Brief Builder', 'nr-brief-builder' ),
			'all_items'          => __( 'All submissions', 'nr-brief-builder' ),
			'view_item'          => __( 'View submission', 'nr-brief-builder' ),
			'search_items'       => __( 'Search submissions', 'nr-brief-builder' ),
			'not_found'          => __( 'No submissions found.', 'nr-brief-builder' ),
			'not_found_in_trash' => __( 'No submissions in the trash.', 'nr-brief-builder' ),
		);

		$args = array(
			'labels'              => $labels,
			'public'              => false,
			'publicly_queryable'  => false,
			'show_ui'             => true,
			'show_in_menu'        => false, // Attached under the Briefify menu, see Briefify_Admin.
			'show_in_admin_bar'   => false,
			'show_in_nav_menus'   => false,
			'show_in_rest'        => false,
			'exclude_from_search' => true,
			'capability_type'     => 'post',
			'map_meta_cap'        => true,
			'capabilities'        => array(
				'create_posts' => 'do_not_allow', // Submissions are only created via the front-end form.
			),
			'hierarchical'        => false,
			'supports'            => array( 'title' ),
			'has_archive'         => false,
			'rewrite'             => false,
			'menu_icon'           => 'dashicons-forms',
		);

		register_post_type( BRIEFIFY_CPT, $args );
	}

	/**
	 * Customize the admin list table columns.
	 *
	 * @param array $columns Existing columns.
	 * @return array
	 */
	public function columns( $columns ) {
		$new_columns = array(
			'cb'                => $columns['cb'],
			'title'             => __( 'Client', 'nr-brief-builder' ),
			'briefify_template' => __( 'Template', 'nr-brief-builder' ),
			'briefify_budget'   => __( 'Budget', 'nr-brief-builder' ),
			'briefify_status'   => __( 'Status', 'nr-brief-builder' ),
			'date'              => __( 'Received', 'nr-brief-builder' ),
		);

		return $new_columns;
	}

	/**
	 * Output custom column content.
	 *
	 * @param string $column  Column key.
	 * @param int    $post_id Post ID.
	 */
	public function render_column( $column, $post_id ) {
		switch ( $column ) {
			case 'briefify_template':
				echo esc_html( get_post_meta( $post_id, '_briefify_template', true ) );
				break;

			case 'briefify_budget':
				$data   = get_post_meta( $post_id, '_briefify_data', true );
				$budget = is_array( $data ) && isset( $data['budget'] ) ? $data['budget'] : '';
				echo esc_html( $budget );
				break;

			case 'briefify_status':
				$status  = get_post_meta( $post_id, '_briefify_status', true );
				$status  = $status ? $status : 'new';
				$default = sprintf( '<span class="briefify-status briefify-status--%1$s">%2$s</span>', esc_attr( $status ), esc_html( briefify_status_label( $status ) ) );

				// Pro (includes/pro/class-briefify-pro-status.php) hooks this
				// to swap the static badge for an editable dropdown. Nothing
				// hooks it in the free version, so $default renders as-is —
				// unchanged from before this filter existed.
				echo apply_filters( 'briefify_status_column_output', $default, $status, $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput -- $default is already escaped above; Pro's replacement is responsible for escaping its own output.
				break;
		}
	}

	/**
	 * Remove the "Quick Edit" action which does not make sense for submissions.
	 *
	 * @param array   $actions Row actions.
	 * @param WP_Post $post    Post object.
	 * @return array
	 */
	public function row_actions( $actions, $post ) {
		if ( BRIEFIFY_CPT === $post->post_type ) {
			unset( $actions['inline hide-if-no-js'] );
		}

		return $actions;
	}
}

/**
 * Get a human readable label for a submission status.
 *
 * @param string $status Status key.
 * @return string
 */
function briefify_status_label( $status ) {
	// Pro (includes/pro/class-briefify-pro-status.php) hooks this to add
	// the rest of the pipeline (In progress / Quoted / Won / Lost). In the
	// free version nothing hooks it, so this stays exactly the 1-entry map
	// it always was.
	$labels = apply_filters(
		'briefify_status_labels',
		array(
			'new' => __( 'New', 'nr-brief-builder' ),
		)
	);

	return isset( $labels[ $status ] ) ? $labels[ $status ] : ucfirst( $status );
}
