<?php
/**
 * Provides static tracker config.
 *
 * @package Ealab\Deployer
 */

namespace EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\TrackerConfig;

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\Config\ConfigProviderBase;

/**
 * Provides static tracker config.
 */
class StaticTrackerConfigProvider extends ConfigProviderBase {
	/**
	 * {@inheritDoc}
	 */
	public function collect(): ?array {
		if ( null !== $this->config ) {
			// The definitions never change for this provider, so we can return the previously collected ones.
			return $this->config;
		}

		// Reset the definitions.
		$this->config = array();

		$this->config['options'] = array(
			'field_definitions_overrides' => array(
				'option_value' => array(
					'value_handler'          => 'option',
					'option_name_field_name' => 'option_name',
				),
			),
			'local_id_field'              => array(
				'name' => 'option_id',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'option_name',
				),
			),
			'tracker_id'                  => 'options',
			'uuid_source_fields'          => array(
				'option_name',
			),
		);

		$this->config['postmeta'] = array(
			'field_definitions_overrides' => array(
				'post_id' => array(
					'value_handler' => 'reference:posts',
				),
			),
			'local_id_field'              => array(
				'name' => 'meta_id',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'meta_key',
				),
			),
			'subordination'               => array(
				'primary_tracker_id'    => 'posts',
				'primary_item_id_field' => 'post_id',
			),
			'tracker_id'                  => 'postmeta',
			'uuid_source_fields'          => array(
				'meta_key',
			),
		);

		$this->config['posts'] = array(
			'field_definitions_overrides' => array(
				'guid'         => array(
					'processors' => array(
						array(
							'id' => 'url_host_name',
						),
					),
				),
				'post_author'  => array(
					'processors' => array(
						array(
							'id' => 'import_author',
						),
					),
				),
				'post_content' => array(
					'processors' => array(
						array(
							'id' => 'url_host_name',
						),
					),
				),
				'post_parent'  => array(
					'value_handler' => 'reference:posts',
				),
			),
			'local_id_field'              => array(
				'name' => 'ID',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'post_title',
				),
			),
			'tracker_id'                  => 'posts',
		);

		$this->config['term_relationships'] = array(
			'field_definitions_overrides' => array(
				'object_id'        => array(
					'value_handler' => 'reference:posts',
				),
				'term_taxonomy_id' => array(
					'value_handler' => 'reference:term_taxonomy',
				),
			),
			'local_id_field'              => array(
				'name' => '["object_id","term_taxonomy_id"]',
				'type' => 'text',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'referenced_item',
				'config' => array(
					'field_name' => 'term_taxonomy_id',
				),
			),
			'subordination'               => array(
				'primary_tracker_id'    => 'posts',
				'primary_item_id_field' => 'object_id',
			),
			'tracker_id'                  => 'term_relationships',
			'uuid_source_fields'          => array(
				'object_id',
				'term_taxonomy_id',
			),
		);

		$this->config['term_taxonomy'] = array(
			'field_definitions_overrides' => array(
				'parent'  => array(
					'value_handler' => 'reference:term_taxonomy',
				),
				'term_id' => array(
					'value_handler' => 'reference:terms',
				),
			),
			'local_id_field'              => array(
				'name' => 'term_taxonomy_id',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'field',
				'config' => array(
					'field_name'        => 'term_id',
					'prefix_field_name' => 'taxonomy',
				),
			),
			'tracker_id'                  => 'term_taxonomy',
			'uuid_source_fields'          => array(
				'taxonomy',
				'term_id',
			),
		);

		$this->config['termmeta'] = array(
			'field_definitions_overrides' => array(
				'term_id' => array(
					'value_handler' => 'reference:terms',
				),
			),
			'local_id_field'              => array(
				'name' => 'meta_id',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'meta_key',
				),
			),
			'subordination'               => array(
				'primary_tracker_id'    => 'terms',
				'primary_item_id_field' => 'term_id',
			),
			'tracker_id'                  => 'termmeta',
			'uuid_source_fields'          => array(
				'meta_key',
			),
		);

		$this->config['terms'] = array(
			'local_id_field'         => array(
				'name' => 'term_id',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                 => '',
			'screen_label_formatter' => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'name',
				),
			),
			'tracker_id'             => 'terms',
		);

		$this->config['usermeta'] = array(
			'field_definitions_overrides' => array(
				'user_id' => array(
					'value_handler' => 'reference:users',
				),
			),
			'local_id_field'              => array(
				'name' => 'umeta_id',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                      => '',
			'screen_label_formatter'      => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'meta_key',
				),
			),
			'subordination'               => array(
				'primary_tracker_id'    => 'users',
				'primary_item_id_field' => 'user_id',
			),
			'tracker_id'                  => 'usermeta',
			'uuid_source_fields'          => array(
				'meta_key',
			),
		);

		$this->config['users'] = array(
			'excluded'               => true,
			'local_id_field'         => array(
				'name' => 'ID',
				'type' => 'bigint(20) unsigned',
			),
			'plugin'                 => '',
			'screen_label_formatter' => array(
				'id'     => 'field',
				'config' => array(
					'field_name' => 'user_nicename',
				),
			),
			'tracker_id'             => 'users',
		);

		return $this->config;
	}
}
