<?php
/**
 * Sync Analytics 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\DTO\Product_Sync_Audit_Request;
use MOWCPS\Common\Models\Product_Sync_Audit_Model;
use MOWCPS\Common\Models\Supplier_Model;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Utils\Model_Utils;

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

/**
 * Class Product_Sync_Audit_Handler
 *
 * Handles sync analytics and reporting operations
 */
class Product_Sync_Audit_Handler {
	/**
	 * Product sync audit model instance
	 *
	 * @var Product_Sync_Audit_Model
	 */
	private $audit_model;

	/**
	 * Supplier model instance
	 *
	 * @var Supplier_Model
	 */
	private $supplier_model;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->audit_model    = new Product_Sync_Audit_Model();
		$this->supplier_model = new Supplier_Model();
	}

	/**
	 * Get product sync audit data by supplier
	 *
	 * @param string $supplier_uuid Supplier UUID.
	 * @return array|WP_Error
	 */
	public function get_product_sync_audit_by_supplier( $supplier_uuid ) {
		// Validate UUID parameter using DTO.
		$validation = Product_Sync_Audit_Request::validate_uuid( $supplier_uuid, 'supplier_uuid' );
		if ( is_wp_error( $validation ) ) {
			return $validation;
		}

		// Get supplier by UUID.
		$supplier = $this->supplier_model->get_by_uuid( $supplier_uuid );
		if ( ! $supplier ) {
			return new WP_Error(
				Constants::ERROR_SUPPLIER_NOT_FOUND,
				'Supplier not found',
				array( 'status' => Constants::HTTP_NOT_FOUND )
			);
		}

		$result = $this->audit_model->get_product_sync_audit_by_supplier( $supplier['id'] );

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

		return Model_Utils::sanitize_for_api_response( $result );
	}
}
