<?php
/**
 * Authorization Handler
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Handlers\Rest_API
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Handlers\Rest_API;

use WP_Error;
use MOWCPS\Common\Models\Authorization_Model;
use MOWCPS\Common\Models\Supplier_Model;
use MOWCPS\Common\DTO\Authorization_Request;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Utils\Model_Utils;

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

/**
 * Class Authorization_Handler
 *
 * Handles authorization REST API operations
 */
class Authorization_Handler {

	/**
	 * Constructor
	 */
	public function __construct() {
	}

	/**
	 * Get authorization for a supplier
	 *
	 * @param string $supplier_uuid Supplier UUID.
	 * @return array|WP_Error
	 */
	public function get_by_supplier_uuid( $supplier_uuid ) {
		// Validate UUID parameter using DTO.
		$validation = Authorization_Request::validate_uuid( $supplier_uuid, 'supplier_uuid' );
		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		// First, get the supplier to validate it exists and get the ID.
		$supplier = Supplier_Model::get_by_uuid( $supplier_uuid );
		if ( ! $supplier ) {
			return new WP_Error(
				Constants::ERROR_SUPPLIER_NOT_FOUND,
				Constants::MSG_SUPPLIER_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		// Get authorization by supplier ID.
		$authorization = Authorization_Model::get_by_supplier_id( $supplier['id'] );
		if ( ! $authorization ) {
			return new WP_Error(
				Constants::ERROR_RECORD_NOT_FOUND,
				Constants::MSG_RECORD_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		// Format data types for API response.
		return Model_Utils::sanitize_for_api_response( $authorization ); // Remove default ID fields.
	}

	/**
	 * Create authorization for a supplier
	 *
	 * @param string|int $supplier_identifier Supplier UUID or ID.
	 * @param array      $data Authorization data.
	 * @return array|WP_Error
	 */
	public function create( $supplier_identifier, $data ) {
		$identifier_info = Model_Utils::identify_identifier( $supplier_identifier );

		if ( is_wp_error( $identifier_info ) ) {
			return $identifier_info;
		}

		if ( 'id' === $identifier_info['type'] ) {
			$supplier_id = $identifier_info['value'];
		} else {
			$supplier = Supplier_Model::get_by_uuid( $identifier_info['value'] );
			if ( ! $supplier ) {
				return new WP_Error(
					Constants::ERROR_SUPPLIER_NOT_FOUND,
					Constants::MSG_SUPPLIER_NOT_FOUND,
					array( 'status' => Constants::HTTP_NOT_FOUND )
				);
			}
			$supplier_id = $supplier['id'];
		}

		// Transform and validate data using DTO.
		$authorization_request = Authorization_Request::from_request_parameters( $data );
		if ( is_wp_error( $authorization_request ) ) {
			return $authorization_request;
		}

		// Add supplier_id to the validated data.
		$validated_data                = $authorization_request->to_array();
		$validated_data['supplier_id'] = $supplier_id;

		$result = Authorization_Model::create( $validated_data );
		if ( is_wp_error( $result ) ) {
			return $result;
		}

		// Format data types for API response.
		return Model_Utils::sanitize_for_api_response( $result ); // Remove default ID fields.
	}

	/**
	 * Update authorization for a supplier
	 *
	 * @param string $supplier_uuid Supplier UUID.
	 * @param array  $data Authorization data.
	 * @return array|WP_Error
	 */
	public function update( $supplier_uuid, $data ) {
		// Validate UUID parameter using DTO.
		$validation = Authorization_Request::validate_uuid( $supplier_uuid, 'supplier_uuid' );
		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		// First, get the supplier to validate it exists and get the ID.
		$supplier = Supplier_Model::get_by_uuid( $supplier_uuid );
		if ( ! $supplier ) {
			return new WP_Error(
				Constants::ERROR_SUPPLIER_NOT_FOUND,
				Constants::MSG_SUPPLIER_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		// Get existing authorization.
		$existing = Authorization_Model::get_by_supplier_id( $supplier['id'] );
		if ( ! $existing ) {
			return new WP_Error(
				Constants::ERROR_RECORD_NOT_FOUND,
				Constants::MSG_RECORD_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		// Transform and validate data using DTO for partial updates.
		if ( ! empty( $data ) ) {
			$authorization_request = Authorization_Request::from_request_parameters( $data );
			if ( is_wp_error( $authorization_request ) ) {
				return $authorization_request;
			}
		}

		$result = Authorization_Model::update_by_supplier_id( $supplier['id'], $data );
		if ( is_wp_error( $result ) ) {
			return $result;
		}

		// Format data types for API response.
		return Model_Utils::sanitize_for_api_response( $result ); // Remove default ID fields.
	}

	/**
	 * Delete authorization for a supplier
	 *
	 * @param string $supplier_uuid Supplier UUID.
	 * @return bool|WP_Error
	 */
	public function delete( $supplier_uuid ) {
		// Validate UUID parameter using DTO.
		$validation = Authorization_Request::validate_uuid( $supplier_uuid, 'supplier_uuid' );
		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		// First, get the supplier to validate it exists and get the ID.
		$supplier = Supplier_Model::get_by_uuid( $supplier_uuid );
		if ( ! $supplier ) {
			return new WP_Error(
				Constants::ERROR_SUPPLIER_NOT_FOUND,
				Constants::MSG_SUPPLIER_NOT_FOUND,
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		return Authorization_Model::delete_by_supplier_id( $supplier['id'] );
	}
}
