<?php
/**
 * Normalized import manifest
 *
 * Canonical internal shape produced by ImportParser from any supported
 * export format (slidemind-export-v1, v2.0 bulk, v1.0 per-item). Items
 * are plain associative arrays (not sub-DTOs) to keep serialization
 * trivial through transients.
 *
 * @package SlideMind\Core
 * @since   0.6.0
 */

namespace SlideMind\Core;

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

class ImportManifest {

	/** @var string One of 'slidemind-export-v1', 'v2.0-bulk', 'v1.0-course', 'v1.0-formation', 'v1.0-module', 'v1.0-slide', 'v1.0-courses', 'v1.0-modules', 'v1.0-slides'. */
	public $source_format = '';

	/** @var string ISO 8601, empty if unknown. */
	public $generated_at = '';

	/** @var string Source site URL, empty if unknown. */
	public $source_site_url = '';

	/** @var string Plugin version at export time, empty if unknown. */
	public $plugin_version = '';

	/** @var array<int, array<string, mixed>> Items keyed positionally; each carries file_ref + existing_match (after analyze_collisions). */
	public $courses = [];

	/** @var array<int, array<string, mixed>> */
	public $modules = [];

	/** @var array<int, array<string, mixed>> */
	public $slides = [];

	/** @var array<int, array<string, mixed>> */
	public $templates = [];

	/** @var array<int, array<string, mixed>> Carried for audit only — never imported. */
	public $memory_variables = [];

	/** @var array<int, array<string, mixed>> */
	public $memory_definitions = [];

	/** @var array<string, mixed> Safe subset of plugin settings; API keys already absent. */
	public $settings = [];

	/**
	 * Convert to a plain array (for transient storage / JSON transport).
	 *
	 * @return array<string, mixed>
	 */
	public function to_array(): array {
		return [
			'source_format'      => $this->source_format,
			'generated_at'       => $this->generated_at,
			'source_site_url'    => $this->source_site_url,
			'plugin_version'     => $this->plugin_version,
			'courses'            => $this->courses,
			'modules'            => $this->modules,
			'slides'             => $this->slides,
			'templates'          => $this->templates,
			'memory_variables'   => $this->memory_variables,
			'memory_definitions' => $this->memory_definitions,
			'settings'           => $this->settings,
		];
	}

	/**
	 * Rebuild a manifest from a previously-serialized array.
	 *
	 * @param array<string, mixed> $data
	 */
	public static function from_array( array $data ): self {
		$manifest = new self();
		foreach ( [ 'source_format', 'generated_at', 'source_site_url', 'plugin_version' ] as $scalar ) {
			$manifest->$scalar = isset( $data[ $scalar ] ) ? (string) $data[ $scalar ] : '';
		}
		foreach ( [ 'courses', 'modules', 'slides', 'templates', 'memory_variables', 'memory_definitions' ] as $list ) {
			$manifest->$list = isset( $data[ $list ] ) && is_array( $data[ $list ] ) ? array_values( $data[ $list ] ) : [];
		}
		$manifest->settings = isset( $data['settings'] ) && is_array( $data['settings'] ) ? $data['settings'] : [];
		return $manifest;
	}
}
