<?php

namespace EasyContentManager\Features;

use EasyContentManager\Utils\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class RegisterTaxonomies {




	public static function init() {
		$self = new self();
		add_action( 'init', [ $self, 'register_custom_taxonomies' ], 9 );

		add_action('init', function () {
			if ( get_option( 'ecm_flush_taxonomy_rules', false ) ) {
				flush_rewrite_rules();
				delete_option( 'ecm_flush_taxonomy_rules' );
			}
		});
	}

	public function register_custom_taxonomies() {
		$taxonomies = Helper::get_all_custom_taxonomies();

		foreach ( $taxonomies as $taxonomy_post ) {
			$data = maybe_unserialize( $taxonomy_post->post_content );

			if ( ! isset( $data['taxonomy_slug'] ) || empty( $data['object_type'] ) ) {
				continue;
			}

			$slug = sanitize_key( $data['taxonomy_slug'] );
			$object_types = array_map( function ( $item ) {
				return $item['post_type'];
			}, (array) $data['object_type'] );

			$args = $this->prepare_taxonomy_args( $data );

			register_taxonomy( $slug, array_merge( [ 'academy_courses', 'storeengine_product' ], $object_types ), $args );
		}

		$this->register_local_taxonomies();
	}

	public function register_local_taxonomies() {
		$taxonomies = ecm_get_json_from_files( ecm_get_plugins_taxonomy_path() );
		$theme_taxonomies = [];

		if ( Helper::has_theme_support() ) {
			$theme_taxonomies = ecm_load_json_files_from_folders( [ Helper::has_theme_support() . '/taxonomies' ] );
		}

		$taxonomies = array_merge( $taxonomies, $theme_taxonomies );

		foreach ( $taxonomies as $taxonomy ) {
			$data = $taxonomy['post_content'];

			if ( ! isset( $data['taxonomy_slug'] ) || empty( $data['object_type'] ) ) {
				continue;
			}

			$slug = $data['taxonomy_slug'];
			$object_types = array_map(function ( $item ) {
				return $item['slug'] ?? $item['title'] ?? $item;
			}, (array) $data['object_type']);

			$args = $this->prepare_taxonomy_args( $data );

			register_taxonomy( $slug, $object_types, $args );
		}
	}

	protected function prepare_taxonomy_args( $data ) {
		$args = [];

		$args['labels'] = $data['labels'] ?? [];
		$args['description'] = $data['description'] ?? '';
		$args['public'] = (bool) ( $data['public'] ?? true );
		$args['publicly_queryable'] = (bool) ( $data['publicly_queryable'] ?? $args['public'] );
		$args['show_ui'] = (bool) ( $data['show_ui'] ?? $args['public'] );
		$args['show_in_menu'] = $data['show_in_menu'] ?? $args['show_ui'];
		$args['show_in_nav_menus'] = $data['show_in_nav_menus'] ?? $args['public'];
		$args['show_tagcloud'] = $data['show_tagcloud'] ?? $args['show_ui'];
		$args['show_in_quick_edit'] = $data['show_in_quick_edit'] ?? $args['show_ui'];
		$args['show_admin_column'] = $data['show_admin_column'] ?? false;
		$args['meta_box_cb'] = $data['meta_box_cb'] ?? null;
		$args['meta_box_sanitize_cb'] = $data['meta_box_sanitize_cb'] ?? null;
		$args['hierarchical'] = (bool) ( $data['hierarchical'] ?? false );
		$args['update_count_callback'] = $data['update_count_callback'] ?? null;
		$args['sort'] = $data['sort'] ?? false;
		$args['args'] = $data['args'] ?? [];

		if ( ! empty( $data['capabilities_enabled'] ) && ! empty( $data['capabilities'] ) ) {
			$args['capabilities'] = $data['capabilities'];
		}

		if ( ! empty( $data['show_in_rest'] ) ) {
			$args['show_in_rest'] = true;
			$args['rest_base'] = $data['rest_base'] ?? $data['taxonomy_slug'];
			$args['rest_namespace'] = $data['rest_namespace'] ?? 'wp/v2';
			$args['rest_controller_class'] = $data['rest_controller_class'] ?? 'WP_REST_Terms_Controller';
		}

		$args['query_var'] = $data['query_var'] ?? $data['taxonomy_slug'];
		if ( 'none' === $args['query_var'] ) {
			$args['query_var'] = false;
		} elseif ( 'custom' === $args['query_var'] && ! empty( $data['query_var_name'] ) ) {
			$args['query_var'] = $data['query_var_name'];
		}

		$args['rewrite'] = $data['rewrite'] ?? true;
		if ( is_array( $args['rewrite'] ) ) {
			$args['rewrite'] = array_merge([
				'slug' => $data['taxonomy_slug'],
				'with_front' => true,
				'hierarchical' => false,
			], $args['rewrite']);
		}

		return apply_filters( 'easy_content_manager/taxonomy/registration_args', $args, $data );
	}
}
