<?php
namespace BwWinner;

class Custom_Post_Types {
	public function __construct () {
		add_action( 'init', [ '\BwWinner\Custom_Post_Types', 'register' ] );
		add_action( 'template_redirect', [ '\BwWinner\Custom_Post_Types', 'redirect' ] );
		add_filter( 'posts_clauses', [ '\BwWinner\Custom_Post_Types', 'brand_post_clauses' ], 10, 2 );
	}
	
	public static function redirect () {
		global $post;
		if (
			is_singular( 'company' )
		) {
			global $wp_query;
			$wp_query->posts = [];
			$wp_query->post = null;
			$wp_query->set_404();
			status_header(404);
			nocache_headers();
		}
	}

	public static function register () {
		// company
		$labels = array(
			'name' => 'Companies',
			'singular_name' => 'Company',
			'add_new' => 'Add New Company',
			'add_new_item' => 'Add New Company',
			'edit_item' => 'Edit Company',
			'new_item' => 'New Company',
			'all_items' => 'All Companies',
			'view_item' => 'View Company',
			'search_items' => 'Search Companies',
			'not_found' =>  'No Company Found',
			'not_found_in_trash' => 'No company found in Trash', 
			'parent_item_colon' => '',
			'menu_name' => 'Companies',
		);
		$args = array(
			'public' => true,
			'labels' => $labels,
			'has_archive' => true,
			'show_ui' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'query_var' => true,
			'show_in_rest' => true,
			'menu_icon' => 'dashicons-businessperson',
			'supports' => array(
				'title',
				'editor',
				'excerpt',
				'revisions',
				'thumbnail',
				'author',
				'page-attributes'
			)
		);
		register_post_type( 'company', $args );
		// brand
		$labels = array(
			'name' => 'Brands',
			'singular_name' => 'Brand',
			'add_new' => 'Add New Brand',
			'add_new_item' => 'Add New Brand',
			'edit_item' => 'Edit Brand',
			'new_item' => 'New Brand',
			'all_items' => 'All Brands',
			'view_item' => 'View Brand',
			'search_items' => 'Search Brands',
			'not_found' =>  'No Brand Found',
			'not_found_in_trash' => 'No brand found in Trash', 
			'parent_item_colon' => '',
			'menu_name' => 'Brands',
		);
		$args = array(
			'public' => true,
			'labels' => $labels,
			'has_archive' => true,
			'show_ui' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'query_var' => true,
			'show_in_rest' => true,
			'menu_icon' => 'dashicons-tag',
			'supports' => array(
				'title',
				'editor',
				'excerpt',
				'revisions',
				'thumbnail',
				'author',
				'page-attributes'
			)
		);
		register_post_type( 'brand', $args );
		// product
		$labels = array(
			'name' => 'Products',
			'singular_name' => 'Product',
			'add_new' => 'Add New Product',
			'add_new_item' => 'Add New Product',
			'edit_item' => 'Edit Product',
			'new_item' => 'New Product',
			'all_items' => 'All Products',
			'view_item' => 'View Product',
			'search_items' => 'Search Products',
			'not_found' =>  'No Product Found',
			'not_found_in_trash' => 'No product found in Trash', 
			'parent_item_colon' => '',
			'menu_name' => 'Products',
		);
		$args = array(
			'public' => true,
			'labels' => $labels,
			'has_archive' => true,
			'show_ui' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'query_var' => true,
			'show_in_rest' => true,
			'menu_icon' => 'dashicons-products',
			'rewrite' => array(
				'slug' => 'product',
			),
			'supports' => array(
				'title',
				'editor',
				'excerpt',
				'revisions',
				'thumbnail',
				'author',
				'page-attributes'
			)
		);
		register_post_type( 'bw-product', $args );
		
		register_taxonomy(
			'product-category', 
			'bw-product', 
			array(
				'hierarchical' => false, 
				'label' => 'Product Category', 
				'query_var' => true,
				'show_in_rest' => true
			)
		);

		// events
		$labels = array(
			'name' => 'Events',
			'singular_name' => 'Event',
			'add_new' => 'Add New Event',
			'add_new_item' => 'Add New Event',
			'edit_item' => 'Edit Event',
			'new_item' => 'New Event',
			'all_items' => 'All Events',
			'view_item' => 'View Event',
			'search_items' => 'Search Events',
			'not_found' =>  'No Event Found',
			'not_found_in_trash' => 'No event found in Trash', 
			'parent_item_colon' => '',
			'menu_name' => 'Events',
		);
		$args = array(
			'labels' => $labels,
			'public' => false,
			'has_archive' => true,
			'show_ui' => true,
			'capability_type' => 'post',
			'hierarchical' => false,
			'query_var' => true,
			'show_in_rest' => true,
			'menu_icon' => 'dashicons-admin-post',
			'supports' => array(
				'title',
				'thumbnail'
			)
		);
		register_post_type( 'events', $args );
	}
	
	public static function brand_post_clauses ( $clauses, $wp_query ) {
		global $wpdb;
		$brand_id = $wp_query->get('brand_id');

		if ($brand_id) {
			$clauses['join'] .= " INNER JOIN {$wpdb->prefix}bw_winners_brand_posts AS bp ON {$wpdb->posts}.ID = bp.post_id ";
			$clauses['where'] .= $wpdb->prepare( " AND bp.brand_id = %d ", $brand_id );
		}

		return $clauses;
	}
}
