<?php
/**
 * Custom post types for the BASCO UAE theme.
 *
 * Registers:
 *   - speaker  : faculty / speakers
 *   - agenda   : sessions on the program
 *   - sponsor  : sponsors / partners
 *
 * @package BASCO_UAE
 */

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

/**
 * Register the Speaker custom post type.
 */
function basco_uae_register_speaker_cpt() {
	$labels = array(
		'name'               => _x( 'Faculty', 'post type general name', 'basco-uae' ),
		'singular_name'      => _x( 'Speaker', 'post type singular name', 'basco-uae' ),
		'menu_name'          => _x( 'Faculty', 'admin menu', 'basco-uae' ),
		'add_new'            => _x( 'Add New', 'speaker', 'basco-uae' ),
		'add_new_item'       => __( 'Add New Speaker', 'basco-uae' ),
		'new_item'           => __( 'New Speaker', 'basco-uae' ),
		'edit_item'          => __( 'Edit Speaker', 'basco-uae' ),
		'view_item'          => __( 'View Speaker', 'basco-uae' ),
		'all_items'          => __( 'All Faculty', 'basco-uae' ),
		'search_items'       => __( 'Search Faculty', 'basco-uae' ),
		'not_found'          => __( 'No faculty found.', 'basco-uae' ),
		'not_found_in_trash' => __( 'No faculty found in Trash.', 'basco-uae' ),
	);

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'has_archive'        => 'faculty',
		'rewrite'            => array( 'slug' => 'faculty' ),
		'menu_icon'          => 'dashicons-businessperson',
		'menu_position'      => 21,
		'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt', 'page-attributes' ),
		'show_in_rest'       => true,
	);

	register_post_type( 'speaker', $args );
}
add_action( 'init', 'basco_uae_register_speaker_cpt' );

/**
 * Register the Agenda custom post type.
 */
function basco_uae_register_agenda_cpt() {
	$labels = array(
		'name'          => _x( 'Agenda', 'post type general name', 'basco-uae' ),
		'singular_name' => _x( 'Session', 'post type singular name', 'basco-uae' ),
		'menu_name'     => _x( 'Agenda', 'admin menu', 'basco-uae' ),
		'add_new_item'  => __( 'Add New Session', 'basco-uae' ),
		'edit_item'     => __( 'Edit Session', 'basco-uae' ),
		'all_items'     => __( 'All Sessions', 'basco-uae' ),
	);

	register_post_type( 'agenda', array(
		'labels'        => $labels,
		'public'        => true,
		'has_archive'   => false,
		'rewrite'       => array( 'slug' => 'session' ),
		'menu_icon'     => 'dashicons-calendar-alt',
		'menu_position' => 22,
		'supports'      => array( 'title', 'editor', 'page-attributes' ),
		'show_in_rest'  => true,
	) );
}
add_action( 'init', 'basco_uae_register_agenda_cpt' );

/**
 * Register the Sponsor custom post type.
 */
function basco_uae_register_sponsor_cpt() {
	$labels = array(
		'name'          => _x( 'Sponsors', 'post type general name', 'basco-uae' ),
		'singular_name' => _x( 'Sponsor', 'post type singular name', 'basco-uae' ),
		'menu_name'     => _x( 'Sponsors', 'admin menu', 'basco-uae' ),
		'add_new_item'  => __( 'Add New Sponsor', 'basco-uae' ),
		'all_items'     => __( 'All Sponsors', 'basco-uae' ),
	);

	register_post_type( 'sponsor', array(
		'labels'        => $labels,
		'public'        => true,
		'has_archive'   => false,
		'rewrite'       => array( 'slug' => 'sponsor' ),
		'menu_icon'     => 'dashicons-awards',
		'menu_position' => 23,
		'supports'      => array( 'title', 'thumbnail', 'page-attributes' ),
		'show_in_rest'  => true,
	) );
}
add_action( 'init', 'basco_uae_register_sponsor_cpt' );

/**
 * Register meta fields for agenda items.
 */
function basco_uae_register_meta() {
	register_post_meta( 'agenda', '_basco_time', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );
	register_post_meta( 'agenda', '_basco_day', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );
	register_post_meta( 'agenda', '_basco_speaker', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );
	register_post_meta( 'agenda', '_basco_topic', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );

	register_post_meta( 'speaker', '_basco_role', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );
	register_post_meta( 'speaker', '_basco_affiliation', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );
	register_post_meta( 'speaker', '_basco_country', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );

	register_post_meta( 'sponsor', '_basco_url', array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'auth_callback' => function() { return current_user_can( 'edit_posts' ); },
	) );
}
add_action( 'init', 'basco_uae_register_meta' );

/**
 * Add classic meta boxes (in case block editor is disabled for the CPT).
 */
function basco_uae_add_meta_boxes() {
	add_meta_box( 'basco-speaker-details', __( 'Speaker Details', 'basco-uae' ), 'basco_uae_speaker_meta_box', 'speaker', 'side' );
	add_meta_box( 'basco-agenda-details', __( 'Session Details', 'basco-uae' ), 'basco_uae_agenda_meta_box', 'agenda', 'side' );
	add_meta_box( 'basco-sponsor-details', __( 'Sponsor Details', 'basco-uae' ), 'basco_uae_sponsor_meta_box', 'sponsor', 'side' );
}
add_action( 'add_meta_boxes', 'basco_uae_add_meta_boxes' );

function basco_uae_speaker_meta_box( $post ) {
	wp_nonce_field( 'basco_speaker_save', 'basco_speaker_nonce' );
	$role        = get_post_meta( $post->ID, '_basco_role', true );
	$affiliation = get_post_meta( $post->ID, '_basco_affiliation', true );
	$country     = get_post_meta( $post->ID, '_basco_country', true );
	?>
	<p><label for="basco_role"><strong><?php esc_html_e( 'Role / Title', 'basco-uae' ); ?></strong></label>
	<input type="text" id="basco_role" name="basco_role" value="<?php echo esc_attr( $role ); ?>" style="width:100%;" /></p>
	<p><label for="basco_affiliation"><strong><?php esc_html_e( 'Affiliation', 'basco-uae' ); ?></strong></label>
	<input type="text" id="basco_affiliation" name="basco_affiliation" value="<?php echo esc_attr( $affiliation ); ?>" style="width:100%;" /></p>
	<p><label for="basco_country"><strong><?php esc_html_e( 'Country', 'basco-uae' ); ?></strong></label>
	<input type="text" id="basco_country" name="basco_country" value="<?php echo esc_attr( $country ); ?>" style="width:100%;" /></p>
	<?php
}

function basco_uae_agenda_meta_box( $post ) {
	wp_nonce_field( 'basco_agenda_save', 'basco_agenda_nonce' );
	$day     = get_post_meta( $post->ID, '_basco_day', true );
	$time    = get_post_meta( $post->ID, '_basco_time', true );
	$speaker = get_post_meta( $post->ID, '_basco_speaker', true );
	$topic   = get_post_meta( $post->ID, '_basco_topic', true );
	?>
	<p><label for="basco_day"><strong><?php esc_html_e( 'Day', 'basco-uae' ); ?></strong></label>
	<select id="basco_day" name="basco_day" style="width:100%;">
		<option value="day-1" <?php selected( $day, 'day-1' ); ?>>Day 1</option>
		<option value="day-2" <?php selected( $day, 'day-2' ); ?>>Day 2</option>
	</select></p>
	<p><label for="basco_time"><strong><?php esc_html_e( 'Time (e.g. 09:00 - 09:30)', 'basco-uae' ); ?></strong></label>
	<input type="text" id="basco_time" name="basco_time" value="<?php echo esc_attr( $time ); ?>" style="width:100%;" /></p>
	<p><label for="basco_speaker"><strong><?php esc_html_e( 'Speaker(s)', 'basco-uae' ); ?></strong></label>
	<input type="text" id="basco_speaker" name="basco_speaker" value="<?php echo esc_attr( $speaker ); ?>" style="width:100%;" /></p>
	<p><label for="basco_topic"><strong><?php esc_html_e( 'Topic / Category', 'basco-uae' ); ?></strong></label>
	<input type="text" id="basco_topic" name="basco_topic" value="<?php echo esc_attr( $topic ); ?>" style="width:100%;" /></p>
	<?php
}

function basco_uae_sponsor_meta_box( $post ) {
	wp_nonce_field( 'basco_sponsor_save', 'basco_sponsor_nonce' );
	$url = get_post_meta( $post->ID, '_basco_url', true );
	?>
	<p><label for="basco_url"><strong><?php esc_html_e( 'Sponsor Website URL', 'basco-uae' ); ?></strong></label>
	<input type="url" id="basco_url" name="basco_url" value="<?php echo esc_attr( $url ); ?>" style="width:100%;" /></p>
	<?php
}

/**
 * Save meta box data.
 */
function basco_uae_save_meta( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}

	if ( isset( $_POST['basco_speaker_nonce'] ) && wp_verify_nonce( $_POST['basco_speaker_nonce'], 'basco_speaker_save' ) ) {
		foreach ( array( 'basco_role', 'basco_affiliation', 'basco_country' ) as $key ) {
			if ( isset( $_POST[ $key ] ) ) {
				update_post_meta( $post_id, '_' . $key, sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) );
			}
		}
	}

	if ( isset( $_POST['basco_agenda_nonce'] ) && wp_verify_nonce( $_POST['basco_agenda_nonce'], 'basco_agenda_save' ) ) {
		foreach ( array( 'basco_day', 'basco_time', 'basco_speaker', 'basco_topic' ) as $key ) {
			if ( isset( $_POST[ $key ] ) ) {
				update_post_meta( $post_id, '_' . $key, sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) );
			}
		}
	}

	if ( isset( $_POST['basco_sponsor_nonce'] ) && wp_verify_nonce( $_POST['basco_sponsor_nonce'], 'basco_sponsor_save' ) ) {
		if ( isset( $_POST['basco_url'] ) ) {
			update_post_meta( $post_id, '_basco_url', esc_url_raw( wp_unslash( $_POST['basco_url'] ) ) );
		}
	}
}
add_action( 'save_post', 'basco_uae_save_meta' );

/**
 * Flush rewrite rules on activation/deactivation (call via hook in functions.php if needed).
 */
function basco_uae_flush_rewrites() {
	basco_uae_register_speaker_cpt();
	basco_uae_register_agenda_cpt();
	basco_uae_register_sponsor_cpt();
	flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'basco_uae_flush_rewrites' );
