<?php
/**
 * @fs_premium_only
 *
 * Inventory Presser preset.
 *
 * Maps common feed fields onto the meta keys and taxonomies that
 * Inventory Presser's `inventory_vehicle` CPT uses. Once applied, the
 * site owner's feed flows directly into Inventory Presser's data model
 * so their existing inventory display, filters, and shortcodes continue
 * to work — our plugin handles the import side, Inventory Presser owns
 * the frontend.
 *
 * Mapping reference: the meta keys and taxonomy slugs below reflect
 * Inventory Presser's public data model as of the latest stable release.
 * If a future Inventory Presser release renames keys, ship a new preset
 * version rather than editing this one in place, so customers locked
 * to an older Inventory Presser version aren't broken.
 *
 * @since 2.5.0
 * @package AutomotiveFeedImport
 */

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

class AFI_Preset_Inventory_Presser extends AFI_Preset_Base
{
	public function get_id()
	{
		return 'inventory_presser';
	}

	public function get_label()
	{
		return __('Inventory Presser', 'automotive-feed-import');
	}

	public function get_target_cpt()
	{
		return 'inventory_vehicle';
	}

	public function detect()
	{
		// Inventory Presser registers its own CPT + a global function
		// invp() that's only present when the plugin is active. We check
		// both so a stale CPT registration alone doesn't trigger a false
		// positive.
		return parent::detect() && function_exists('invp');
	}

	/**
	 * Categorical fields go to Inventory Presser's taxonomies — that's
	 * how their frontend builds filterable archives. Order is significant:
	 * model-year before model so "ModelYear" doesn't accidentally match
	 * "model" first.
	 */
	public function get_taxonomy_map()
	{
		return array(
			array(
				'synonyms' => array('make', 'manufacturer', 'brand'),
				'taxonomy' => 'make',
			),
			array(
				'synonyms' => array('modelyear', 'year', 'modelyr'),
				'taxonomy' => 'model-year',
			),
			array(
				'synonyms' => array('model', 'modelname'),
				'taxonomy' => 'model',
			),
			array(
				'synonyms' => array('bodystyle', 'bodytype', 'style', 'body'),
				'taxonomy' => 'body-style',
			),
			array(
				'synonyms' => array('fuel', 'fueltype', 'fuelkind'),
				'taxonomy' => 'fuel',
			),
			array(
				'synonyms' => array('transmission', 'trans', 'gearbox'),
				'taxonomy' => 'transmission',
			),
			array(
				'synonyms' => array('drivetype', 'drive', 'drivetrain', 'driveline'),
				'taxonomy' => 'drive-type',
			),
			array(
				'synonyms' => array(
					'condition', 'designation', 'newused', 'newusedindicator',
				),
				'taxonomy' => 'condition',
			),
			array(
				'synonyms' => array('availability', 'status', 'inventorystatus'),
				'taxonomy' => 'availability',
			),
			array(
				'synonyms' => array(
					'location', 'lotlocation', 'lot', 'dealerlocation',
				),
				'taxonomy' => 'location',
			),
			array(
				'synonyms' => array('cylinders', 'cyl', 'enginecylinders'),
				'taxonomy' => 'cylinders',
			),
		);
	}

	/**
	 * Non-categorical fields land on Inventory Presser's prefixed
	 * meta keys. Numeric/text fields that don't fit a taxonomy live here.
	 */
	public function get_meta_map()
	{
		return array(
			// Map the unique/stock identifier to our internal stock_number
			// key — the importer keys off it to match a vehicle on the next
			// sync. (Inventory Presser also exposes its own stock field; a
			// future preset version can mirror the value into it for native
			// display. Mapping here to stock_number is what makes the sync
			// itself reliable, which takes priority.)
			array(
				'synonyms' => array(
					'stocknumber', 'stockno', 'stockid', 'stock', 'inventoryid', 'dealerno',
				),
				'target' => 'stock_number',
			),
			array(
				'synonyms' => array('vin', 'vehicleidentificationnumber'),
				'target'   => 'inventory_presser_vin',
			),
			array(
				'synonyms' => array('odometer', 'mileage', 'miles', 'milesdriven'),
				'target'   => 'inventory_presser_odometer',
			),
			array(
				'synonyms' => array(
					'price', 'specialwebprice', 'webprice', 'listprice', 'askingprice',
				),
				'target' => 'inventory_presser_price',
			),
			array(
				'synonyms' => array('msrp', 'factorylist', 'factorylistprice'),
				'target'   => 'inventory_presser_msrp',
			),
			array(
				'synonyms' => array(
					'color', 'extcolor', 'exteriorcolor', 'exteriorcolorname',
				),
				'target' => 'inventory_presser_color',
			),
			array(
				'synonyms' => array(
					'interiorcolor', 'intcolor', 'interiorcolorname', 'trim',
				),
				'target' => 'inventory_presser_interior_color',
			),
			array(
				'synonyms' => array('engine', 'enginedescription', 'enginedetail'),
				'target'   => 'inventory_presser_engine',
			),
			array(
				'synonyms' => array('description', 'comments', 'notes', 'longdesc'),
				'target'   => 'inventory_presser_description',
			),
			array(
				'synonyms' => array('doors', 'doorcount', 'numberofdoors'),
				'target'   => 'inventory_presser_doors',
			),
		);
	}
}

// Register on load. The file is only loaded by the main plugin when
// Pro is active, so this line never runs in the free build.
afi_get_preset_registry()->register(new AFI_Preset_Inventory_Presser());
