<?php
/**
 * Enqueue child styles.
 */
function child_enqueue_styles () {
	wp_enqueue_style('child-theme', get_stylesheet_directory_uri() . '/style.css', array(), 100);
}

add_action('wp_enqueue_scripts', 'child_enqueue_styles');

add_filter('tribe_events_editor_default_template', function($template) {
	$template = array(
		array('tribe/event-datetime'),
		array('core/paragraph', array('placeholder' => 'Add Description...')),
		array('tribe/event-price'),
		array('tribe/event-organizer'),
		array('tribe/event-venue'),
		array('tribe/event-website'),
		array('tribe/event-links'),
		array('acf/bw-course-content'),
		array('tribe/related-events', array('title' => 'Related Courses'))
	);
	return $template;
}, 51, 1);

include_once 'blocks/index.php';
include_once 'includes/course-shortcodes.php';
include_once 'includes/block-editor-assets.php';
include_once 'includes/admin-assets.php';

// update cities/agencies when a venue is updated
function bw_venue_updated_update_events ($venue_id, $data) {
	if (empty($venue_id)) return;
	$args = array(
		'post_type'      => 'tribe_events',
		'posts_per_page' => -1,
		'tax_query'      => array(
			'relation' => 'AND'
		),
		'meta_query'     => array(
			'_EventVenueID_clause' => array(
				'key'     => '_EventVenueID',
				'value'   =>  $venue_id,
				'compare' => '='
			)
		)
	);
	$results = new WP_Query($args);
	while ($results->have_posts()) {
		$results->the_post();
		if (isset($data['City'])) {
			update_field('city', $data['City'], get_the_ID());
		}
	}
}
add_action('tribe_events_venue_updated', 'bw_venue_updated_update_events', 20, 2);

// update city when a venue is selected
function bw_venue_changed_update_events ($event_id, $data, $event) {
	if (empty($venue_id = get_field('_EventVenueID', $event_id))) return;
	update_field('city', tribe_get_city($venue_id), $event_id);
	update_field('agency', get_field('agency', $venue_id), $event_id);
}
add_action('tribe_events_update_meta', 'bw_venue_changed_update_events', 20, 3);

function bw_clean_course_edit () {
	remove_meta_box('tagsdiv-post_tag', 'tribe_events', 'side');
	remove_meta_box('cmcal_calendardiv', 'tribe_events', 'side');

	remove_meta_box('commentstatusdiv', 'tribe_events', 'normal');
	remove_meta_box('commentsdiv', 'tribe_events', 'normal');
	remove_meta_box('codemine_event_tribe_events_event_metaboxes', 'tribe_events', 'normal');
	remove_meta_box('codemine_event_tribe_events_event_metaboxes', 'tribe_events', 'advanced');
	remove_meta_box('codemine_event_tribe_events_event_metaboxes', 'tribe_events', 'side');
}
add_action('add_meta_boxes', 'bw_clean_course_edit');

function bw_update_event_agency ($value, $post_id, $field) {
	// if a venue's agency is updated
    if (get_post_type($post_id) === 'tribe_venue' && $field['name'] == 'agency') {
        $args = array(
			'post_type'      => 'tribe_events',
			'posts_per_page' => -1,
			'tax_query'      => array(
				'relation' => 'AND'
			),
			'meta_query'     => array(
				'_EventVenueID_clause' => array(
					'key'     => '_EventVenueID',
					'value'   =>  $post_id,
					'compare' => '='
				)
			)
		);
		$results = new WP_Query($args);
		while ($results->have_posts()) {
			$results->the_post();
			update_field('agency', $value, get_the_ID());
		}
    }
    return $value;
    
}
add_filter('acf/update_value', 'bw_update_event_agency', 10, 3);
