<?php
/**
 * Suppliers REST API Loader
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Rest_APIs
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Rest_APIs;

use WP_REST_Server;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Controllers\Rest_API\Supplier_Rest_Controller;

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

/**
 * Class Suppliers_APIs
 *
 * Registers all Suppliers REST API controllers and routes
 */
class Suppliers_APIs {

	/**
	 * Register all routes
	 *
	 * @return void
	 */
	public static function register_routes() {

		$controller = new Supplier_Rest_Controller();

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/import',
			array(
				array(
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => array( $controller, 'import_bulk_supplier' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/export',
			array(
				array(
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => array( $controller, 'export_bulk_suppliers' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_items' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
				array(
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => array( $controller, 'create_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		// Test connection.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/test',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'test_supplier' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Fetch SNS Activewear payment profiles.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/payment-profiles',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'get_payment_profiles' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Delete products for selected suppliers.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/delete-products',
			array(
				'methods'             => WP_REST_Server::DELETABLE,
				'callback'            => array( $controller, 'bulk_delete_products' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Get suppliers that have synced products (for deletion modal).
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/with-products',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $controller, 'get_suppliers_with_products' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $controller, 'get_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
				array(
					'methods'             => WP_REST_Server::EDITABLE,
					'callback'            => array( $controller, 'update_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
				array(
					'methods'             => WP_REST_Server::DELETABLE,
					'callback'            => array( $controller, 'delete_item' ),
					'permission_callback' => array( $controller, 'check_permissions' ),
				),
			)
		);

		// Debug log operations.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/debug-logs/download',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $controller, 'download_debug_logs' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/debug-logs/clear',
			array(
				'methods'             => WP_REST_Server::DELETABLE,
				'callback'            => array( $controller, 'clear_debug_logs' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// File download endpoint.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/download-file',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $controller, 'download_file' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		// Audit log operations.
		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/audit-logs',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $controller, 'get_audit_logs' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/audit-logs/(?P<file_index>\d+)/download',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => array( $controller, 'download_audit_log' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/audit-logs/finalize',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $controller, 'finalize_audit_logs' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);

		register_rest_route(
			Constants::REST_API_NAMESPACE,
			'/suppliers/(?P<supplier_uuid>[a-zA-Z0-9-]+)/audit-logs/clear',
			array(
				'methods'             => WP_REST_Server::DELETABLE,
				'callback'            => array( $controller, 'clear_audit_logs' ),
				'permission_callback' => array( $controller, 'check_permissions' ),
			)
		);
	}
}
