<?php
/**
 * File Path: includes/api/class-mbs-onix-importer.php
 *
 * ONIX 3.0 Importer -- My Book Showroom
 *
 * Thin dispatcher that coordinates MBS_Onix_Parser and MBS_Onix_Reconciler
 * to import an ONIX 3.0 XML string into the book catalog.
 *
 * Public API (unchanged):
 *   $result = ( new MBS_Onix_Importer() )->import( $xml_string );
 *
 * Caller must verify nonce, mbs_is_pub(), current_user_can(), and MIME type
 * before calling import(). See class-mbs-tab-onix.php.
 *
 * Parsing logic:      MBS_Onix_Parser       (includes/api/class-mbs-onix-parser.php)
 * Reconcile/write:    MBS_Onix_Reconciler   (includes/api/class-mbs-onix-reconciler.php)
 *
 * Spec: SPRINT-ONIX-EXPORT-IMPORT-Spec.md §8
 * Security: SECURITY.md Rules 3, 4, 5; mbs_is_pub() gate enforced by caller.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Api
 * @since      1.1.0
 */

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

class MBS_Onix_Importer { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Parse and import an ONIX XML string.
	 *
	 * Caller must verify nonce, mbs_is_pub(), current_user_can(), and MIME type.
	 *
	 * @param string $xml_string Raw XML content.
	 * @return array{
	 *   created:  int,
	 *   updated:  int,
	 *   appended: int,
	 *   skipped:  int,
	 *   notices:  string[],
	 *   errors:   string[],
	 * }
	 */
	public function import( string $xml_string ): array {
		$stats = array(
			'created'  => 0,
			'updated'  => 0,
			'appended' => 0,
			'skipped'  => 0,
			'notices'  => array(),
			'errors'   => array(),
		);

		$products = ( new MBS_Onix_Parser() )->parse( $xml_string, $stats );
		if ( ! empty( $products ) ) {
			( new MBS_Onix_Reconciler() )->reconcile( $products, $stats );
		}

		return $stats;
	}
}
