<?php 

function wpsgv__cpt() {

	$labels = array(
		'name'                  => _x( 'WP Google Street View', 'Post Type General Name', 'wp-google-street-view' ),
		'singular_name'         => _x( 'WP Google Street View', 'Post Type Singular Name', 'wp-google-street-view' ),
		'menu_name'             => __( 'WP Google Street View', 'wp-google-street-view' ),
		'name_admin_bar'        => __( 'WP Google Street View', 'wp-google-street-view' ),
		'parent_item_colon'     => __( 'Parent Item:', 'wp-google-street-view' ),
		'all_items'             => __( 'All Items', 'wp-google-street-view' ),
		'add_new_item'          => __( 'Add New Map', 'wp-google-street-view' ),
		'add_new'               => __( 'Add New', 'wp-google-street-view' ),
		'new_item'              => __( 'New Item', 'wp-google-street-view' ),
		'edit_item'             => __( 'Edit Item', 'wp-google-street-view' ),
		'update_item'           => __( 'Update Item', 'wp-google-street-view' ),
		'not_found'             => __( 'Not found', 'wp-google-street-view' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'wp-google-street-view' ),
		'featured_image'        => __( 'Featured Image', 'wp-google-street-view' ),
		'set_featured_image'    => __( 'Set featured image', 'wp-google-street-view' ),
		'remove_featured_image' => __( 'Remove featured image', 'wp-google-street-view' ),
		'use_featured_image'    => __( 'Use as featured image', 'wp-google-street-view' ),
		'insert_into_item'      => __( 'Insert into item', 'wp-google-street-view' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'wp-google-street-view' ),
		'items_list'            => __( 'Items list', 'wp-google-street-view' ),
		'items_list_navigation' => __( 'Items list navigation', 'wp-google-street-view' ),
		'filter_items_list'     => __( 'Filter items list', 'wp-google-street-view' ),
	);
	$args = array(
		'label'                 => __( 'WP Google Street View', 'wp-google-street-view' ),
		'description'           => __( 'WP Google Street View Post Type', 'wp-google-street-view' ),
		'labels'                => $labels,
		'supports'              => array( 'title' ),
		'hierarchical'          => false,
		'public'                => false,
		'show_ui'               => true,
		'show_in_menu'          => true,
        'menu_position'         => 10,
        'menu_icon'             => 'dashicons-location',
		'show_in_admin_bar'     => true,
		'show_in_nav_menus'     => false,
		'can_export'            => true,
		'has_archive'           => false,
		'exclude_from_search'   => true,
		'publicly_queryable'    => false,
		'capability_type'       => 'page',
	);
	register_post_type( 'wpgsv', $args );

}
add_action( 'init', 'wpsgv__cpt', 0 );

function wpgsv__register_taxonomy() {
    
    $labels = array(
        'name'              => _x( 'Map Categories', 'taxonomy general name', 'wp-google-street-view' ),
        'singular_name'     => _x( 'Map Category', 'taxonomy singular name', 'wp-google-street-view' ),
        'search_items'      => __( 'Search Categories', 'wp-google-street-view' ),
        'all_items'         => __( 'All Categories', 'wp-google-street-view' ),
        'parent_item'       => __( 'Parent Category', 'wp-google-street-view' ),
        'parent_item_colon' => __( 'Parent Category:', 'wp-google-street-view' ),
        'edit_item'         => __( 'Edit Category', 'wp-google-street-view' ),
        'update_item'       => __( 'Update Category', 'wp-google-street-view' ),
        'add_new_item'      => __( 'Add New Category', 'wp-google-street-view' ),
        'new_item_name'     => __( 'New Category Name', 'wp-google-street-view' ),
        'menu_name'         => __( 'Categories', 'wp-google-street-view' ),
    );

    $args = array(
        'hierarchical'      => true, // Like categories (true) or tags (false)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true, // Show in admin list table
        'query_var'         => true,
        'show_in_nav_menus' => false,
        'show_tagcloud'     => false,
        'rewrite'           => array( 'slug' => 'wpgsv-category' ),
        'capabilities'      => array(
            'manage_terms' => 'manage_options',
            'edit_terms'   => 'manage_options',
            'delete_terms' => 'manage_options',
            'assign_terms' => 'edit_posts',
        ),
    );

    register_taxonomy( 'wpgsv_category', array( 'wpgsv' ), $args );
}

// Hook it to init action with priority 0 (same as post type registration)
add_action( 'init', 'wpgsv__register_taxonomy', 0 );

// Optional: Add category filter dropdown to admin list
function wpgsv__add_category_filter() {
    global $typenow;
    
    if ( $typenow == 'wpgsv' ) {
        $taxonomy = 'wpgsv_category';
        $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
        $info_taxonomy = get_taxonomy($taxonomy);
        
        wp_dropdown_categories(array(
            'show_option_all' => sprintf( __( 'Show all %s', 'wp-google-street-view' ), $info_taxonomy->label ),
            'taxonomy'        => $taxonomy,
            'name'            => $taxonomy,
            'orderby'         => 'name',
            'selected'        => $selected,
            'hierarchical'    => $info_taxonomy->hierarchical,
            'value_field'     => 'slug',
            'show_count'      => true,
            'hide_empty'      => false,
        ));
    }
}
add_action( 'restrict_manage_posts', 'wpgsv__add_category_filter' );

// Make the filter work
function wpgsv__category_filter_query( $query ) {
    global $pagenow;
    $type = 'wpgsv';
    
    if ( $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == $type && isset($_GET['wpgsv_category']) && $_GET['wpgsv_category'] != '' ) {
        $query->query_vars['wpgsv_category'] = $_GET['wpgsv_category'];
    }
}
add_filter( 'parse_query', 'wpgsv__category_filter_query' );

// Optional: Add category column to admin list (already enabled by 'show_admin_column' => true above)
// But you can customize the column display if needed:
function wpgsv__custom_category_column_content( $content, $column_name, $term_id ) {
    if ( $column_name !== 'wpgsv_category' ) {
        return $content;
    }
    
    $term = get_term( $term_id, 'wpgsv_category' );
    if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
        $content .= esc_attr( $term->name );
    }
    
    return $content;
}
add_filter( 'manage_wpgsv_category_custom_column', 'wpgsv__custom_category_column_content', 10, 3 );

require WPGSV_PLUGIN_PATH . '/admin/metabox.php';