<?php

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

/**
 * Register meta boxes for the 'stpo_collection' custom post type.
 *
 * @since      1.0.0
 * @package    Storyposts
 * @subpackage Storyposts/includes/meta
 */
class Storyposts_Collection_Meta {

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		add_action( 'storyposts_cmb2_admin_init', array( $this, 'register_metaboxes' ) );
	}

	/**
	 * Get admin stories (collection context) for the dropdown.
	 *
	 * @since    1.0.0
	 * @return   array   Array of story options (ID => title with preview info)
	 */
	private function get_collection_stories() {
		$stories = get_posts( array(
			'post_type'      => 'stpo_story',
			'posts_per_page' => -1,
			'post_status'    => 'publish',
			'meta_query'     => array(
				'relation' => 'OR',
				// Stories explicitly marked as 'collection'
				array(
					'key'     => '_story_context',
					'value'   => 'collection',
					'compare' => '=',
				),
				// Stories with no context set (legacy/default to collection for admin)
				array(
					'key'     => '_story_context',
					'compare' => 'NOT EXISTS',
				),
			),
			'orderby'        => 'title',
			'order'          => 'ASC',
		) );

		$story_options = array();
		if ( $stories ) {
			foreach ( $stories as $story ) {
				/* translators: %d: Story ID */
				$title = ! empty( $story->post_title ) ? $story->post_title : sprintf( esc_html__( '(No title - ID: %d)', 'storyposts' ), $story->ID );
				$story_options[ $story->ID ] = $title;
			}
		}

		return $story_options;
	}

	/**
	 * Register the meta boxes.
	 *
	 * @since    1.0.0
	 */
	public function register_metaboxes() {

		$prefix = '_stpo_';

		/**
		 * Collection Stories Metabox
		 */
		$cmb = new_storyposts_cmb2_box( array(
			'id'            => $prefix . 'collection_stories_metabox',
			'title'         => esc_html__( 'Collection Stories', 'storyposts' ),
			'object_types'  => array( 'stpo_collection' ),
			'context'       => 'normal',
			'priority'      => 'high',
			'show_names'    => true,
		) );

		// Get collection stories
		$story_options = $this->get_collection_stories();

		// Info text if no stories exist
		if ( empty( $story_options ) ) {
			$cmb->add_field( array(
				'name' => '',
				'desc' => wp_kses(
					sprintf(
						'<div style="padding: 15px; background: #fff3cd; border-left: 4px solid #ffc107; margin: 10px 0;">
							<strong>%s</strong><br>%s <a href="%s">%s</a>
						</div>',
						esc_html__( 'No stories available!', 'storyposts' ),
						esc_html__( 'Create some stories first, then come back to add them to this collection.', 'storyposts' ),
						esc_url( admin_url( 'post-new.php?post_type=stpo_story' ) ),
						esc_html__( 'Create a Story →', 'storyposts' )
					),
					array(
						'div'    => array( 'style' => array() ),
						'strong' => array(),
						'br'     => array(),
						'a'      => array( 'href' => array() ),
					)
				),
				'id'   => $prefix . 'no_stories_notice',
				'type' => 'title',
			) );
		}

		$cmb->add_field( array(
			'name'             => esc_html__( 'Select Stories', 'storyposts' ),
			'desc'             => esc_html__( 'Select stories to include in this collection. Drag rows to reorder. Only "Collection" context stories are shown.', 'storyposts' ),
			'id'               => $prefix . 'story_ids',
			'type'             => 'select',
			'show_option_none' => esc_html__( '— Select a Story —', 'storyposts' ),
			'options'          => $story_options,
			'repeatable'       => true,
			'text'             => array(
				'add_row_text' => esc_html__( '+ Add Another Story', 'storyposts' ),
			),
			'attributes'       => array(
				'style' => 'width: 100%; max-width: 500px;',
			),
		) );

		/**
		 * Collection Settings Metabox
		 */
		$settings_cmb = new_storyposts_cmb2_box( array(
			'id'            => $prefix . 'collection_settings_metabox',
			'title'         => esc_html__( 'Collection Settings', 'storyposts' ),
			'object_types'  => array( 'stpo_collection' ),
			'context'       => 'side',
			'priority'      => 'default',
			'show_names'    => true,
		) );

		// Collection Status
		$settings_cmb->add_field( array(
			'name'    => esc_html__( 'Status', 'storyposts' ),
			'id'      => $prefix . 'collection_status',
			'type'    => 'select',
			'default' => 'active',
			'options' => array(
				'active'   => esc_html__( 'Active', 'storyposts' ),
				'inactive' => esc_html__( 'Inactive', 'storyposts' ),
			),
		) );

		// Collection Order (for display priority)
		$settings_cmb->add_field( array(
			'name'       => esc_html__( 'Display Order', 'storyposts' ),
			'desc'       => esc_html__( 'Lower numbers display first.', 'storyposts' ),
			'id'         => $prefix . 'collection_order',
			'type'       => 'text',
			'default'    => '0',
			'attributes' => array(
				'type' => 'number',
				'min'  => '0',
			),
		) );

	}

}
