<?php

namespace WPKIN_SHOP_MANAGER\API\AdminApi;

if ( ! defined( 'ABSPATH' ) ) {
	wp_die( esc_html__( 'You can\'t access this page', 'shop-manager' ) );
}

/**
 * Admin Routers
 *
 * @since 1.0.0
 */
class Routers extends Callback {

	/**
	 * Routers class constructor
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'rest_api_init', [ $this, 'wpkin_admin_register_api_init' ] );
		add_action( 'woocommerce_new_order',            [ $this, 'wpkin_sm_clear_dashboard_cache' ] );
		add_action( 'woocommerce_order_status_changed', [ $this, 'wpkin_sm_clear_dashboard_cache' ] );
	}

	public function wpkin_admin_register_api_init() {
		register_rest_route(
            'shop-manager/v1', '/order-statuses', [
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_admin_order_statuses' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/save-admin-settings', [
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_save_admin_settings' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/get-admin-settings', [
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_admin_settings' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/get-order-list', [
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_order_list' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
        );

		register_rest_route(
			'shop-manager/v1',
			'/get-order-counts',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_order_counts' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Routes for trash operations
		register_rest_route(
			'shop-manager/v1',
			'/trash-order',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_trash_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/permanently-delete-order',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_permanently_delete_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/restore-order',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_restore_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Routes for bulk operations
		register_rest_route(
			'shop-manager/v1',
			'/bulk-trash-orders',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_bulk_trash_orders' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/bulk-delete-orders',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_bulk_delete_orders' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/bulk-restore-orders',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_bulk_restore_orders' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/bulk-update-order-status',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_bulk_update_order_status' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/order-filter-references',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_order_filter_references' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Single order actions
		register_rest_route(
			'shop-manager/v1',
			'/update-order-status',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_update_order_status' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Customer orders
		register_rest_route(
			'shop-manager/v1',
			'/customer-orders/(?P<id>\d+)',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_customer_orders' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Route for exporting orders
		register_rest_route(
			'shop-manager/v1',
			'/export-orders',
			[
				'methods' => [ 'GET', 'POST' ],
				'callback' => [ $this, 'wpkin_export_orders' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ], // Allow access to everyone initially
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/get-products',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_products' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/create-order',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_create_wc_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/get-countries',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_countries' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/get-states',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_states' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
            'shop-manager/v1', '/orders/(?P<id>\d+)', [
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/orders/(?P<id>\d+)', [
				'methods' => 'PUT',
				'callback' => [ $this, 'wpkin_update_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/delete-order/(?P<id>\d+)', [
				'methods' => 'DELETE',
				'callback' => [ $this, 'wpkin_sm_delete_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
				'args' => [
					'id' => [
						'validate_callback' => function ( $param ) {
							return is_numeric( $param );
						},
					],
					'force' => [
						'required' => false,
						'default' => false,
						'type' => 'boolean',
					],
				],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/dashboard-stats', [
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_sm_get_dashboard_stats' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
				'args' => [
					'start_date' => [
						'required' => false,
						'type' => 'string',
						'validate_callback' => function ( $param ) {
							return strtotime( $param ) !== false;
						},
					],
					'end_date' => [
						'required' => false,
						'type' => 'string',
						'validate_callback' => function ( $param ) {
							return strtotime( $param ) !== false;
						},
					],
				],
			]
        );

		register_rest_route(
            'shop-manager/v1', '/search', [
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_sm_handle_search' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
				'args' => [
					'q' => [
						'required' => true,
						'type' => 'string',
						'sanitize_callback' => 'sanitize_text_field',
					],
					'page' => [
						'required' => false,
						'type' => 'integer',
						'default' => 1,
					],
					'per_page' => [
						'required' => false,
						'type' => 'integer',
						'default' => 10,
					],
				],
			]
        );

		// Products

		register_rest_route(
			'shop-manager/v1',
			'/products/add-new-product',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_add_product' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/products/update-product',
			[
				'methods' => 'PUT',
				'callback' => [ $this, 'wpkin_sm_update_product' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/products',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_sm_get_products' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_field('product_variation', 'global_unique_id', [
			'get_callback' => function($object) {
				return get_post_meta($object['id'], '_global_unique_id', true);
			},
			'update_callback' => function($value, $object) {
				update_post_meta($object->ID, '_global_unique_id', $value);
			},
			'schema' => [
				'description' => __('Global Unique ID (GTIN/UPC/EAN/ISBN)', 'shop-manager'),
				'type' => 'string',
			],
		]);

		register_rest_route(
			'shop-manager/v1',
			'/products/delete',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_delete_products' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/products/duplicate/(?P<id>\d+)',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_sm_duplicate_product' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/product-attributes/(?P<id>\d+)/terms',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_sm_get_attribute_terms' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/custom-fields',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_get_custom_fields' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/custom-fields',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_save_custom_field' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Product Taxonomies
		register_rest_route(
			'shop-manager/v1',
			'/products/delete-taxonomies',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_delete_taxonomies' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/product/get-taxonomies',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_sm_get_taxonomies' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/product/add-taxonomy',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_add_taxonomy' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/product/set-default-category',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_set_default_category' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/product/update-taxonomy-order',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_update_taxonomy_order' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Customers
		register_rest_route(
			'shop-manager/v1',
			'/customers',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_customers' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_customer_profile' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)',
			[
				'methods'             => 'PUT',
				'callback'            => [ $this, 'wpkin_update_customer' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)/orders',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_customer_order_history' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)/notes',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_customer_notes' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)/notes',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_add_customer_note' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)/tags',
			[
				'methods'             => 'PUT',
				'callback'            => [ $this, 'wpkin_update_customer_tags' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/(?P<id>\d+)/tags/refresh',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_refresh_customer_tags' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/insights',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_sm_get_insights' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/insights/(?P<id>[a-zA-Z0-9_]+)/dismiss',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_sm_dismiss_insight' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/insights/reset-dismissed',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_sm_reset_dismissed_insights' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/customers/tags/refresh-all',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_refresh_all_customer_tags' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Reviews
		register_rest_route(
			'shop-manager/v1',
			'/product/get-reviews',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_sm_get_reviews' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Get review counts endpoint
		register_rest_route(
			'shop-manager/v1',
			'/product/get-review-counts',
			[
				'methods' => 'GET',
				'callback' => [ $this, 'wpkin_sm_get_review_counts' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Update review status endpoint
		register_rest_route(
			'shop-manager/v1',
			'/product/update-review-status',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_update_review_status' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Reply to review endpoint
		register_rest_route(
			'shop-manager/v1',
			'/product/reply-to-review',
			[
				'methods' => 'POST',
				'callback' => [ $this, 'wpkin_sm_reply_to_review' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Employees
		register_rest_route(
			'shop-manager/v1',
			'/employees',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_employees' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_create_employee' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/(?P<id>\d+)',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_employee' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/(?P<id>\d+)',
			[
				'methods'             => 'PUT',
				'callback'            => [ $this, 'wpkin_update_employee' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/(?P<id>\d+)',
			[
				'methods'             => 'DELETE',
				'callback'            => [ $this, 'wpkin_delete_employee' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/(?P<id>\d+)/reset-password',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_reset_employee_password' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/roles',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_employee_roles' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/roles',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_create_employee_role' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/roles/(?P<key>[a-zA-Z0-9_]+)',
			[
				'methods'             => 'DELETE',
				'callback'            => [ $this, 'wpkin_delete_employee_role' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/roles/(?P<key>[a-zA-Z0-9_]+)/capabilities',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_update_employee_role' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/roles/(?P<key>[a-zA-Z0-9_]+)',
			[
				'methods'             => 'PATCH',
				'callback'            => [ $this, 'wpkin_toggle_employee_role_visibility' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/access',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_employee_access' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/access',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_save_employee_access' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/(?P<id>\d+)/activity',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_employee_activity' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		register_rest_route(
			'shop-manager/v1',
			'/employees/(?P<id>\d+)/performance-summary',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'wpkin_get_employee_performance_summary' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
			]
		);

		// Bulk resend email
		register_rest_route(
			'shop-manager/v1',
			'/orders/bulk-email',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_bulk_resend_email' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
				'args'                => [
					'ids'        => [ 'required' => true ],
					'email_type' => [ 'required' => true, 'sanitize_callback' => 'sanitize_key' ],
				],
			]
		);

		// Resend order email
		register_rest_route(
			'shop-manager/v1',
			'/orders/(?P<id>\d+)/email',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'wpkin_resend_order_email' ],
				'permission_callback' => [ $this, 'wpkin_admin_check_permissions' ],
				'args'                => [
					'id' => [ 'validate_callback' => fn( $v ) => is_numeric( $v ) ],
				],
			]
		);
	}

}
