<?php

namespace RadiusTheme\RadiusBooking\ExportImport\Importers;

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

use RadiusTheme\RadiusBooking\ExportImport\Contracts\ImporterInterface;
use RadiusTheme\RadiusBooking\ExportImport\IdMapper;
use RadiusTheme\RadiusBooking\Models\Location;

/**
 * Class LocationImporter
 *
 * Imports locations with ID remapping.
 *
 * @package RadiusTheme\RadiusBooking\ExportImport\Importers
 */
class LocationImporter implements ImporterInterface {

	/**
	 * {@inheritDoc}
	 */
	public function getModuleName(): string {
		return 'location';
	}

	/**
	 * {@inheritDoc}
	 */
	public function import( array $records, IdMapper $id_mapper ): int {
		$imported = 0;

		foreach ( $records as $record ) {
			$old_id = (int) ( $record['id'] ?? 0 );
			unset( $record['id'] );
			unset( $record['created_at'], $record['updated_at'] );

			// Image IDs are site-specific, clear them.
			$record['image_id'] = null;

			$model = Location::create( $record );

			if ( $model && $old_id ) {
				$id_mapper->addMapping( 'location', $old_id, (int) $model->id );
				++$imported;
			}
		}

		return $imported;
	}

	/**
	 * {@inheritDoc}
	 */
	public function getDependencies(): array {
		return array();
	}
}
