<?php
/**
 * Locations Custom Post Type and Taxonomy registration.
 *
 * @package SEOvaultSEO\LocalSEO
 */

namespace SEOvaultSEO\LocalSEO;

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

class PostType {

	const POST_TYPE   = 'seovault_locations';
	const TAXONOMY    = 'seovault_locations_category';
	const OPTION_SLUG = 'seovault_locations_slug';

	public static function get_post_type() {
		return apply_filters( 'seovaultseo_local_post_type', self::POST_TYPE );
	}

	public static function get_taxonomy() {
		return apply_filters( 'seovaultseo_local_taxonomy', self::TAXONOMY );
	}

	public static function get_slug() {
		$slug = get_option( 'seovaultseo_local_slug', 'locations' );
		return apply_filters( 'seovaultseo_local_slug', $slug );
	}

	public static function init() {
		add_action( 'init', array( __CLASS__, 'register_post_type' ), 9 );
		add_action( 'init', array( __CLASS__, 'register_taxonomy' ), 9 );
	}

	public static function register_post_type() {
		$pt = self::get_post_type();

		if ( post_type_exists( $pt ) ) {
			return;
		}

		$slug = self::get_slug();

		$labels = array(
			'name'               => __( 'Locations', 'seovault-ai' ),
			'singular_name'      => __( 'Location', 'seovault-ai' ),
			'add_new'            => __( 'New Location', 'seovault-ai' ),
			'add_new_item'       => __( 'Add New Location', 'seovault-ai' ),
			'edit_item'          => __( 'Edit Location', 'seovault-ai' ),
			'new_item'           => __( 'New Location', 'seovault-ai' ),
			'view_item'          => __( 'View Location', 'seovault-ai' ),
			'search_items'       => __( 'Search Locations', 'seovault-ai' ),
			'not_found'          => __( 'No locations found', 'seovault-ai' ),
			'not_found_in_trash' => __( 'No locations found in trash', 'seovault-ai' ),
		);

		$args = array(
			'labels'          => $labels,
			'public'          => true,
			'show_ui'         => true,
			'capability_type' => 'post',
			'hierarchical'    => false,
			'has_archive'     => true,
			'rewrite'         => array(
				'slug'       => sanitize_title( $slug ),
				'with_front' => apply_filters( 'seovaultseo_local_cpt_with_front', true ),
			),
			'menu_icon'       => 'dashicons-location',
			'query_var'       => true,
			'show_in_rest'    => true,
			'rest_base'       => $pt,
			'supports'        => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields' ),
		);

		$args = apply_filters( 'seovaultseo_local_cpt_args', $args );

		register_post_type( $pt, $args );
	}

	public static function register_taxonomy() {
		$tax = self::get_taxonomy();
		$pt  = self::get_post_type();

		if ( taxonomy_exists( $tax ) ) {
			return;
		}

		$labels = array(
			'name'          => __( 'Location Categories', 'seovault-ai' ),
			'singular_name' => __( 'Location Category', 'seovault-ai' ),
			'search_items'  => __( 'Search Location Categories', 'seovault-ai' ),
			'all_items'     => __( 'All Location Categories', 'seovault-ai' ),
			'edit_item'     => __( 'Edit Location Category', 'seovault-ai' ),
			'update_item'   => __( 'Update Location Category', 'seovault-ai' ),
			'add_new_item'  => __( 'Add New Location Category', 'seovault-ai' ),
			'new_item_name' => __( 'New Location Category Name', 'seovault-ai' ),
			'menu_name'     => __( 'Categories', 'seovault-ai' ),
		);

		$args = array(
			'labels'            => $labels,
			'public'            => true,
			'hierarchical'      => true,
			'show_ui'           => true,
			'show_admin_column' => true,
			'query_var'         => true,
			'rewrite'           => array(
				'slug'       => 'location-category',
				'with_front' => true,
			),
			'show_in_rest'      => true,
		);

		$args = apply_filters( 'seovaultseo_local_taxonomy_args', $args );

		register_taxonomy( $tax, array( $pt ), $args );
	}
}
