<?php
/**
 * Reusable launch templates.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Templates;

use ScheduledChanges\Extension\PublicHooks;

/**
 * Scriptable catalog of reusable launch template blueprints.
 */
final class LaunchTemplateCatalog {
	/**
	 * Returns every built-in template blueprint.
	 *
	 * @return array<int,array<string,mixed>>
	 */
	public function all(): array {
		return PublicHooks::launchTemplates(
			array(
				$this->template(
					'wordpress-post-publication',
					'WordPress post publication',
					'Plan a post status and publication date change.',
					array(
						$this->field( 'wordpress-core', 'post', 'wordpress.status', 'set_field' ),
						$this->field( 'wordpress-core', 'post', 'wordpress.publish_date', 'set_field' ),
					)
				),
				$this->template(
					'wordpress-post-content',
					'WordPress post content',
					'Update a post title, content, and excerpt through the native provider.',
					array(
						$this->field( 'wordpress-core', 'post', 'wordpress.title', 'set_field' ),
						$this->field( 'wordpress-core', 'post', 'wordpress.content', 'set_field' ),
						$this->field( 'wordpress-core', 'post', 'wordpress.excerpt', 'set_field' ),
					)
				),
				$this->template(
					'wordpress-page-structure',
					'WordPress page structure',
					'Update page hierarchy, menu order, and page template.',
					array(
						$this->field( 'wordpress-core', 'page', 'wordpress.parent', 'set_field' ),
						$this->field( 'wordpress-core', 'page', 'wordpress.menu_order', 'set_field' ),
						$this->field( 'wordpress-core', 'page', 'wordpress.page_template', 'set_field' ),
					)
				),
			)
		);
	}

	/**
	 * Finds one template by ID.
	 *
	 * @param string $id Template ID.
	 * @return array<string,mixed>|null
	 */
	public function find( string $id ): ?array {
		foreach ( $this->all() as $template ) {
			if ( $id === (string) $template['id'] ) {
				return $template;
			}
		}

		return null;
	}

	/**
	 * Builds a template array.
	 *
	 * @param string                          $id Template ID.
	 * @param string                          $name Template name.
	 * @param string                          $description Template description.
	 * @param array<int,array<string,string>> $operations Operation blueprints.
	 * @return array<string,mixed>
	 */
	private function template(
		string $id,
		string $name,
		string $description,
		array $operations
	): array {
		return array(
			'schema_version' => 1,
			'id'             => $id,
			'name'           => $name,
			'description'    => $description,
			'operations'     => $operations,
		);
	}

	/**
	 * Builds a field-operation blueprint.
	 *
	 * @param string $provider_id Provider ID.
	 * @param string $target_type Target type.
	 * @param string $field_path Canonical field path.
	 * @param string $operation_type Operation type.
	 * @return array<string,string>
	 */
	private function field( string $provider_id, string $target_type, string $field_path, string $operation_type ): array {
		return array(
			'provider_id'    => $provider_id,
			'target_type'    => $target_type,
			'field_path'     => $field_path,
			'operation_type' => $operation_type,
		);
	}
}
