<?php
/**
 * Streams management: list + add/edit form.
 *
 * The form is sectioned into collapsible cards, shows only what applies
 * (pagination params only when pagination is on, WooCommerce fields only
 * when WooCommerce exists), and never asks for JSON — the taxonomy and
 * meta maps are repeater rows serialized by datastream-admin-form.js.
 *
 * @package DataStream
 */

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

// Included from DataStream_Admin -- method-local, not plugin globals.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals

$ds_streams  = DataStream_Streams::get_streams();
$ds_editing  = isset( $_GET['stream'] ) ? sanitize_key( wp_unslash( $_GET['stream'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$ds_stream   = '' !== $ds_editing ? ( $ds_streams[ $ds_editing ] ?? null ) : null;
$ds_is_new   = null === $ds_stream;
$ds_stream   = $ds_stream ?? DataStream_Streams::defaults();
$ds_premium  = DataStream_Feature_Gate::is_premium();
$ds_cap      = DataStream_Feature_Gate::max_enabled_streams();
$ds_dao      = new DataStream_DAO();
$ds_counts   = $ds_dao->get_stream_counts();
$ds_advanced = isset( $_GET['advanced'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$ds_use_wizard = $ds_is_new && ! $ds_advanced;
$ds_tax_map  = json_decode( (string) $ds_stream['taxonomy_map'], true ) ?: array();
$ds_meta_map = json_decode( (string) $ds_stream['meta_map'], true ) ?: array();
$ds_taxes    = get_taxonomies( array( 'public' => true ), 'objects' );
?>

<?php if ( ! empty( $ds_streams ) ) : ?>
	<h2><?php esc_html_e( 'Streams', 'datastream-api-sync' ); ?></h2>
	<p class="description">
		<?php
		printf(
			/* translators: %d: number of streams that will sync */
			esc_html__( 'Each stream is one connected API or feed. Your plan syncs up to %d enabled streams.', 'datastream-api-sync' ),
			(int) $ds_cap
		);
		?>
	</p>
	<table class="widefat striped">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Stream', 'datastream-api-sync' ); ?></th>
				<th><?php esc_html_e( 'Address', 'datastream-api-sync' ); ?></th>
				<th><?php esc_html_e( 'Records', 'datastream-api-sync' ); ?></th>
				<th><?php esc_html_e( 'Status', 'datastream-api-sync' ); ?></th>
				<th><?php esc_html_e( 'Shortcode', 'datastream-api-sync' ); ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ( $ds_streams as $ds_row ) : ?>
				<tr>
					<td>
						<strong><?php echo esc_html( $ds_row['label'] ?: $ds_row['id'] ); ?></strong>
						<br /><code><?php echo esc_html( $ds_row['id'] ); ?></code>
					</td>
					<td><code><?php echo esc_html( $ds_row['api_url'] ); ?></code></td>
					<td><?php echo esc_html( (string) ( $ds_counts[ $ds_row['id'] ] ?? 0 ) ); ?></td>
					<td>
						<?php if ( empty( $ds_row['enabled'] ) ) : ?>
							<?php esc_html_e( 'Paused', 'datastream-api-sync' ); ?>
						<?php elseif ( (int) $ds_row['backoff_until'] > time() ) : ?>
							<?php
							printf(
								/* translators: %s: UTC time */
								esc_html__( 'Waiting — retries at %s UTC', 'datastream-api-sync' ),
								esc_html( gmdate( 'H:i', (int) $ds_row['backoff_until'] ) )
							);
							?>
						<?php else : ?>
							<?php esc_html_e( 'Active', 'datastream-api-sync' ); ?>
						<?php endif; ?>
					</td>
					<td><code>[datastream_list stream="<?php echo esc_attr( $ds_row['id'] ); ?>"]</code></td>
					<td>
						<a class="button button-small" href="<?php echo esc_url( admin_url( 'admin.php?page=datastream-settings&tab=streams&stream=' . $ds_row['id'] ) ); ?>"><?php esc_html_e( 'Edit', 'datastream-api-sync' ); ?></a>
						<a class="button button-small button-link-delete"
							href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=datastream_delete_stream&stream=' . $ds_row['id'] ), 'datastream_delete_stream_' . $ds_row['id'] ) ); ?>"
							onclick="return confirm( '<?php echo esc_js( __( 'Delete this stream? Its synced records are kept until you purge them.', 'datastream-api-sync' ) ); ?>' );">
							<?php esc_html_e( 'Delete', 'datastream-api-sync' ); ?>
						</a>
					</td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
<?php endif; ?>

<?php if ( $ds_use_wizard ) : ?>
	<h2><?php esc_html_e( 'Connect an API', 'datastream-api-sync' ); ?></h2>
	<?php include DATASTREAM_PLUGIN_DIR . 'admin/views/wizard.php'; ?>
	<?php return; ?>
<?php endif; ?>

<h2><?php echo $ds_is_new ? esc_html__( 'Add Stream (Advanced)', 'datastream-api-sync' ) : esc_html__( 'Edit Stream', 'datastream-api-sync' ); ?></h2>
<?php if ( $ds_is_new ) : ?>
	<p class="description"><a href="<?php echo esc_url( admin_url( 'admin.php?page=datastream-settings&tab=streams' ) ); ?>">&larr; <?php esc_html_e( 'Use the guided wizard instead', 'datastream-api-sync' ); ?></a></p>
<?php endif; ?>

<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="datastream-stream-form" id="datastream-stream-form">
	<input type="hidden" name="action" value="datastream_save_stream" />
	<?php wp_nonce_field( 'datastream_save_stream' ); ?>

	<details class="ds-card" open>
		<summary><?php esc_html_e( 'Connection', 'datastream-api-sync' ); ?></summary>
		<div class="ds-card-body">
			<p>
				<?php if ( $ds_is_new ) : ?>
					<label for="ds-id"><?php esc_html_e( 'Stream ID', 'datastream-api-sync' ); ?></label><br />
					<input type="text" id="ds-id" name="stream[id]" class="regular-text" placeholder="products" required pattern="[a-z0-9_\-]+" />
					<span class="description"><?php esc_html_e( 'Lowercase letters, numbers, underscores — used in shortcodes. Cannot be changed later.', 'datastream-api-sync' ); ?></span>
				<?php else : ?>
					<label><?php esc_html_e( 'Stream ID', 'datastream-api-sync' ); ?></label><br />
					<code><?php echo esc_html( $ds_stream['id'] ); ?></code>
					<input type="hidden" name="stream[id]" value="<?php echo esc_attr( $ds_stream['id'] ); ?>" />
				<?php endif; ?>
			</p>
			<p>
				<label for="ds-label"><?php esc_html_e( 'Name', 'datastream-api-sync' ); ?></label><br />
				<input type="text" id="ds-label" name="stream[label]" class="regular-text" value="<?php echo esc_attr( $ds_stream['label'] ); ?>" placeholder="<?php esc_attr_e( 'Products from our supplier', 'datastream-api-sync' ); ?>" />
			</p>
			<p>
				<input type="hidden" name="stream[enabled]" value="0" />
				<label><input type="checkbox" name="stream[enabled]" value="1" <?php checked( ! empty( $ds_stream['enabled'] ) ); ?> /> <?php esc_html_e( 'Sync this stream on the schedule', 'datastream-api-sync' ); ?></label>
			</p>
			<p>
				<label for="ds-url"><?php esc_html_e( 'API address (URL)', 'datastream-api-sync' ); ?></label><br />
				<input type="url" id="ds-url" name="stream[api_url]" class="large-text" value="<?php echo esc_attr( $ds_stream['api_url'] ); ?>" placeholder="https://api.example.com/v1/products" required />
			</p>
			<p>
				<button type="button" class="button" id="datastream-stream-preview" data-stream="<?php echo esc_attr( $ds_is_new ? '' : $ds_stream['id'] ); ?>"><?php esc_html_e( 'Preview & Detect Fields', 'datastream-api-sync' ); ?></button>
				<span id="datastream-preview-note" class="description"><?php echo $ds_is_new ? esc_html__( 'Save the stream first, then preview to see what the API returns.', 'datastream-api-sync' ) : ''; ?></span>
			</p>
			<div id="datastream-preview-result" aria-live="polite"></div>
			<p>
				<label for="ds-method"><?php esc_html_e( 'Request type', 'datastream-api-sync' ); ?></label>
				<select id="ds-method" name="stream[api_method]">
					<option value="GET" <?php selected( $ds_stream['api_method'], 'GET' ); ?>>GET</option>
					<option value="POST" <?php selected( $ds_stream['api_method'], 'POST' ); ?>>POST</option>
				</select>
				<label for="ds-format" style="margin-left:1em"><?php esc_html_e( 'Response format', 'datastream-api-sync' ); ?></label>
				<select id="ds-format" name="stream[format]">
					<?php foreach ( array( 'auto' => __( 'Auto-detect', 'datastream-api-sync' ), 'json' => 'JSON', 'xml' => 'XML', 'rss' => 'RSS / Atom', 'csv' => 'CSV / TSV' ) as $ds_fmt => $ds_fmt_label ) : ?>
						<option value="<?php echo esc_attr( $ds_fmt ); ?>" <?php selected( $ds_stream['format'] ?? 'auto', $ds_fmt ); ?>><?php echo esc_html( $ds_fmt_label ); ?></option>
					<?php endforeach; ?>
				</select>
			</p>
		</div>
	</details>

	<details class="ds-card">
		<summary><?php esc_html_e( 'Authentication', 'datastream-api-sync' ); ?> <span class="ds-card-hint"><?php esc_html_e( 'only if the API needs a key or login', 'datastream-api-sync' ); ?></span></summary>
		<div class="ds-card-body">
			<p>
				<label for="ds-token"><?php esc_html_e( 'Bearer token', 'datastream-api-sync' ); ?></label><br />
				<input type="password" id="ds-token" name="stream[api_bearer_token]" class="regular-text" value="<?php echo esc_attr( '' !== (string) $ds_stream['api_bearer_token'] ? '********' : '' ); ?>" autocomplete="new-password" />
				<?php if ( '' !== (string) $ds_stream['api_bearer_token'] ) : ?>
					<label class="ds-clear"><input type="checkbox" name="stream[api_bearer_token_clear]" value="1" /> <?php esc_html_e( 'Clear saved token', 'datastream-api-sync' ); ?></label>
				<?php endif; ?>
			</p>
			<p>
				<label for="ds-basic-user"><?php esc_html_e( 'Basic auth username', 'datastream-api-sync' ); ?></label><br />
				<input type="text" id="ds-basic-user" name="stream[api_basic_user]" class="regular-text" value="<?php echo esc_attr( $ds_stream['api_basic_user'] ); ?>" autocomplete="off" />
			</p>
			<p>
				<label for="ds-basic-pass"><?php esc_html_e( 'Basic auth password', 'datastream-api-sync' ); ?></label><br />
				<input type="password" id="ds-basic-pass" name="stream[api_basic_pass]" class="regular-text" value="<?php echo esc_attr( '' !== (string) $ds_stream['api_basic_pass'] ? '********' : '' ); ?>" autocomplete="new-password" />
				<?php if ( '' !== (string) $ds_stream['api_basic_pass'] ) : ?>
					<label class="ds-clear"><input type="checkbox" name="stream[api_basic_pass_clear]" value="1" /> <?php esc_html_e( 'Clear saved password', 'datastream-api-sync' ); ?></label>
				<?php endif; ?>
			</p>
			<p>
				<label for="ds-key-param"><?php esc_html_e( 'API key in the address (parameter name / value)', 'datastream-api-sync' ); ?></label><br />
				<input type="text" id="ds-key-param" name="stream[api_key_param]" class="small-text" style="width:10em" value="<?php echo esc_attr( $ds_stream['api_key_param'] ); ?>" placeholder="api_key" autocomplete="off" />
				<input type="text" name="stream[api_key_value]" class="regular-text" value="<?php echo esc_attr( $ds_stream['api_key_value'] ); ?>" autocomplete="off" aria-label="<?php esc_attr_e( 'API key value', 'datastream-api-sync' ); ?>" />
			</p>
			<p>
				<label for="ds-headers"><?php esc_html_e( 'Custom headers (JSON, for unusual schemes)', 'datastream-api-sync' ); ?></label><br />
				<textarea id="ds-headers" name="stream[api_headers]" rows="2" class="large-text code" placeholder='{"X-Api-Key":"your-key"}'><?php echo esc_textarea( $ds_stream['api_headers'] ); ?></textarea>
			</p>
			<?php if ( $ds_premium ) : ?>
				<p>
					<input type="hidden" name="stream[_oauth_open]" value="" />
					<label><input type="checkbox" id="ds-oauth-toggle" <?php checked( '' !== (string) $ds_stream['oauth_token_url'] ); ?> /> <?php esc_html_e( 'This API uses OAuth 2.0', 'datastream-api-sync' ); ?></label>
				</p>
				<div id="ds-oauth-fields" <?php echo '' === (string) $ds_stream['oauth_token_url'] ? 'hidden' : ''; ?>>
					<p>
						<label for="ds-oauth-url"><?php esc_html_e( 'Token URL', 'datastream-api-sync' ); ?></label><br />
						<input type="url" id="ds-oauth-url" name="stream[oauth_token_url]" class="regular-text" value="<?php echo esc_attr( $ds_stream['oauth_token_url'] ); ?>" placeholder="https://api.example.com/oauth/token" />
					</p>
					<p>
						<label for="ds-oauth-id"><?php esc_html_e( 'Client ID', 'datastream-api-sync' ); ?></label><br />
						<input type="text" id="ds-oauth-id" name="stream[oauth_client_id]" class="regular-text" value="<?php echo esc_attr( $ds_stream['oauth_client_id'] ); ?>" autocomplete="off" />
					</p>
					<p>
						<label for="ds-oauth-secret"><?php esc_html_e( 'Client secret', 'datastream-api-sync' ); ?></label><br />
						<input type="password" id="ds-oauth-secret" name="stream[oauth_client_secret]" class="regular-text" value="<?php echo esc_attr( '' !== (string) $ds_stream['oauth_client_secret'] ? '********' : '' ); ?>" autocomplete="new-password" />
						<?php if ( '' !== (string) $ds_stream['oauth_client_secret'] ) : ?>
							<label class="ds-clear"><input type="checkbox" name="stream[oauth_client_secret_clear]" value="1" /> <?php esc_html_e( 'Clear saved secret', 'datastream-api-sync' ); ?></label>
						<?php endif; ?>
					</p>
					<p>
						<label for="ds-oauth-scope"><?php esc_html_e( 'Scope (optional)', 'datastream-api-sync' ); ?></label>
						<input type="text" id="ds-oauth-scope" name="stream[oauth_scope]" class="regular-text" value="<?php echo esc_attr( $ds_stream['oauth_scope'] ); ?>" autocomplete="off" />
						<label for="ds-oauth-refresh" style="margin-left:1em"><?php esc_html_e( 'Refresh token (optional)', 'datastream-api-sync' ); ?></label>
						<input type="password" id="ds-oauth-refresh" name="stream[oauth_refresh_token]" class="regular-text" value="<?php echo esc_attr( '' !== (string) $ds_stream['oauth_refresh_token'] ? '********' : '' ); ?>" autocomplete="new-password" />
					</p>
				</div>
			<?php endif; ?>
			<p class="description"><?php esc_html_e( 'Secret fields showing ******** keep their saved value unless you tick Clear or type a new one.', 'datastream-api-sync' ); ?></p>
		</div>
	</details>

	<details class="ds-card">
		<summary><?php esc_html_e( 'Where the records live', 'datastream-api-sync' ); ?> <span class="ds-card-hint"><?php esc_html_e( 'usually auto-detected', 'datastream-api-sync' ); ?></span></summary>
		<div class="ds-card-body">
			<p>
				<label for="ds-path"><?php esc_html_e( 'Path to the list of records', 'datastream-api-sync' ); ?></label><br />
				<input type="text" id="ds-path" name="stream[json_items_path]" class="regular-text" value="<?php echo esc_attr( $ds_stream['json_items_path'] ); ?>" placeholder="data.items" />
				<span class="description"><?php esc_html_e( 'Dot path, exactly as the API spells it. Leave empty to auto-detect.', 'datastream-api-sync' ); ?></span>
			</p>
			<p>
				<label for="ds-idfield"><?php esc_html_e( 'Which field identifies a record', 'datastream-api-sync' ); ?></label><br />
				<input type="text" id="ds-idfield" name="stream[id_field]" class="regular-text" value="<?php echo esc_attr( $ds_stream['id_field'] ); ?>" placeholder="id" />
				<input type="hidden" name="stream[id_source]" value="field" />
				<label style="margin-left:1em"><input type="checkbox" name="stream[id_source]" value="hash" <?php checked( 'hash' === ( $ds_stream['id_source'] ?? 'field' ) ); ?> /> <?php esc_html_e( 'No unique field — fingerprint each record instead', 'datastream-api-sync' ); ?></label>
			</p>
			<p>
				<label for="ds-contentfield"><?php esc_html_e( 'Keep only this field as the record (optional)', 'datastream-api-sync' ); ?></label><br />
				<input type="text" id="ds-contentfield" name="stream[content_field]" class="regular-text" value="<?php echo esc_attr( $ds_stream['content_field'] ); ?>" />
			</p>
		</div>
	</details>

	<details class="ds-card">
		<summary><?php esc_html_e( 'More pages', 'datastream-api-sync' ); ?> <span class="ds-card-hint"><?php esc_html_e( 'for APIs that split results across pages', 'datastream-api-sync' ); ?></span></summary>
		<div class="ds-card-body">
			<p>
				<label for="ds-pagination"><?php esc_html_e( 'How the API splits pages', 'datastream-api-sync' ); ?></label>
				<select id="ds-pagination" name="stream[pagination]">
					<option value="none" <?php selected( $ds_stream['pagination'], 'none' ); ?>><?php esc_html_e( 'It does not — one request gets everything', 'datastream-api-sync' ); ?></option>
					<option value="page" <?php selected( $ds_stream['pagination'], 'page' ); ?>><?php esc_html_e( 'By page number (?page=2)', 'datastream-api-sync' ); ?></option>
					<option value="offset" <?php selected( $ds_stream['pagination'], 'offset' ); ?>><?php esc_html_e( 'By offset (?offset=100)', 'datastream-api-sync' ); ?></option>
				</select>
			</p>
			<div data-ds-show-pagination <?php echo 'none' === $ds_stream['pagination'] ? 'hidden' : ''; ?>>
				<p>
					<label for="ds-page-param"><?php esc_html_e( 'Parameter name', 'datastream-api-sync' ); ?></label>
					<input type="text" id="ds-page-param" name="stream[page_param]" class="small-text" style="width:8em" value="<?php echo esc_attr( $ds_stream['page_param'] ); ?>" placeholder="page" />
					<label for="ds-perpage-param"><?php esc_html_e( 'Per-page parameter', 'datastream-api-sync' ); ?></label>
					<input type="text" id="ds-perpage-param" name="stream[per_page_param]" class="small-text" style="width:8em" value="<?php echo esc_attr( $ds_stream['per_page_param'] ); ?>" placeholder="per_page" />
					<label for="ds-perpage"><?php esc_html_e( 'Records per page', 'datastream-api-sync' ); ?></label>
					<input type="number" id="ds-perpage" name="stream[per_page]" class="small-text" min="1" max="1000" value="<?php echo esc_attr( (string) $ds_stream['per_page'] ); ?>" />
					<label for="ds-maxpages"><?php esc_html_e( 'Fetch at most (pages)', 'datastream-api-sync' ); ?></label>
					<input type="number" id="ds-maxpages" name="stream[max_pages]" class="small-text" min="1" max="<?php echo esc_attr( (string) DataStream_Feature_Gate::max_pages() ); ?>" value="<?php echo esc_attr( (string) $ds_stream['max_pages'] ); ?>" />
				</p>
				<p class="description">
					<?php
					printf(
						/* translators: %d: maximum pages per sync */
						esc_html__( 'Fetching stops early when a page comes back empty or repeats. Your plan fetches up to %d pages per sync.', 'datastream-api-sync' ),
						(int) DataStream_Feature_Gate::max_pages()
					);
					?>
				</p>
			</div>
		</div>
	</details>

	<?php if ( $ds_premium ) : ?>
	<details class="ds-card" <?php echo '' !== (string) $ds_stream['post_type'] ? 'open' : ''; ?>>
		<summary><?php esc_html_e( 'Turn records into posts', 'datastream-api-sync' ); ?> <span class="ds-card-hint"><?php esc_html_e( 'pages, posts, WooCommerce products…', 'datastream-api-sync' ); ?></span></summary>
		<div class="ds-card-body">
			<p>
				<label for="ds-posttype"><?php esc_html_e( 'Create content of type', 'datastream-api-sync' ); ?></label>
				<select id="ds-posttype" name="stream[post_type]">
					<option value="" <?php selected( $ds_stream['post_type'], '' ); ?>><?php esc_html_e( 'Off — records stay in DataStream only', 'datastream-api-sync' ); ?></option>
					<?php foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $ds_pt ) : ?>
						<?php if ( 'attachment' === $ds_pt->name ) { continue; } ?>
						<option value="<?php echo esc_attr( $ds_pt->name ); ?>" <?php selected( $ds_stream['post_type'], $ds_pt->name ); ?>><?php echo esc_html( $ds_pt->labels->singular_name . ' (' . $ds_pt->name . ')' ); ?></option>
					<?php endforeach; ?>
				</select>
			</p>
			<div data-ds-show-posts <?php echo '' === (string) $ds_stream['post_type'] ? 'hidden' : ''; ?>>
				<p>
					<label for="ds-title-field"><?php esc_html_e( 'Title comes from field', 'datastream-api-sync' ); ?></label>
					<input type="text" id="ds-title-field" name="stream[post_title_field]" class="regular-text" value="<?php echo esc_attr( $ds_stream['post_title_field'] ); ?>" placeholder="name" />
					<label for="ds-content-field2" style="margin-left:1em"><?php esc_html_e( 'Content comes from field', 'datastream-api-sync' ); ?></label>
					<input type="text" id="ds-content-field2" name="stream[post_content_field]" class="regular-text" value="<?php echo esc_attr( $ds_stream['post_content_field'] ); ?>" placeholder="description" />
					<label for="ds-post-status" style="margin-left:1em"><?php esc_html_e( 'Publish as', 'datastream-api-sync' ); ?></label>
					<select id="ds-post-status" name="stream[post_status]">
						<?php foreach ( array( 'publish' => __( 'Published', 'datastream-api-sync' ), 'draft' => __( 'Draft', 'datastream-api-sync' ), 'pending' => __( 'Pending review', 'datastream-api-sync' ), 'private' => __( 'Private', 'datastream-api-sync' ) ) as $ds_status => $ds_status_label ) : ?>
							<option value="<?php echo esc_attr( $ds_status ); ?>" <?php selected( $ds_stream['post_status'], $ds_status ); ?>><?php echo esc_html( $ds_status_label ); ?></option>
						<?php endforeach; ?>
					</select>
				</p>
				<p>
					<label for="ds-image-field"><?php esc_html_e( 'Featured image from field', 'datastream-api-sync' ); ?></label>
					<input type="text" id="ds-image-field" name="stream[image_field]" class="regular-text" value="<?php echo esc_attr( $ds_stream['image_field'] ); ?>" placeholder="image" />
					<input type="hidden" name="stream[sideload_images]" value="0" />
					<label style="margin-left:1em"><input type="checkbox" name="stream[sideload_images]" value="1" <?php checked( ! empty( $ds_stream['sideload_images'] ) ); ?> /> <?php esc_html_e( 'Download images into the Media Library', 'datastream-api-sync' ); ?></label>
				</p>

				<fieldset class="ds-repeater" data-ds-repeater="taxonomy" data-ds-target="ds-taxonomy-json">
					<legend><?php esc_html_e( 'Categories & tags', 'datastream-api-sync' ); ?></legend>
					<p class="description"><?php esc_html_e( 'Put the value of a field into categories, tags, or any taxonomy. Terms are created as needed; "Clothing > Shirts" builds a hierarchy.', 'datastream-api-sync' ); ?></p>
					<div class="ds-repeater-rows">
						<?php foreach ( $ds_tax_map as $ds_tax => $ds_field ) : ?>
							<p class="ds-repeater-row">
								<select class="ds-rep-key" aria-label="<?php esc_attr_e( 'Taxonomy', 'datastream-api-sync' ); ?>">
									<?php foreach ( $ds_taxes as $ds_taxonomy ) : ?>
										<option value="<?php echo esc_attr( $ds_taxonomy->name ); ?>" <?php selected( $ds_tax, $ds_taxonomy->name ); ?>><?php echo esc_html( $ds_taxonomy->labels->name . ' (' . $ds_taxonomy->name . ')' ); ?></option>
									<?php endforeach; ?>
								</select>
								<span class="ds-rep-arrow" aria-hidden="true">←</span>
								<input type="text" class="ds-rep-value regular-text" value="<?php echo esc_attr( $ds_field ); ?>" aria-label="<?php esc_attr_e( 'Field name', 'datastream-api-sync' ); ?>" placeholder="category" />
								<button type="button" class="button-link ds-rep-remove" aria-label="<?php esc_attr_e( 'Remove row', 'datastream-api-sync' ); ?>">&times;</button>
							</p>
						<?php endforeach; ?>
					</div>
					<template class="ds-repeater-template">
						<p class="ds-repeater-row">
							<select class="ds-rep-key" aria-label="<?php esc_attr_e( 'Taxonomy', 'datastream-api-sync' ); ?>">
								<?php foreach ( $ds_taxes as $ds_taxonomy ) : ?>
									<option value="<?php echo esc_attr( $ds_taxonomy->name ); ?>"><?php echo esc_html( $ds_taxonomy->labels->name . ' (' . $ds_taxonomy->name . ')' ); ?></option>
								<?php endforeach; ?>
							</select>
							<span class="ds-rep-arrow" aria-hidden="true">←</span>
							<input type="text" class="ds-rep-value regular-text" value="" aria-label="<?php esc_attr_e( 'Field name', 'datastream-api-sync' ); ?>" placeholder="category" />
							<button type="button" class="button-link ds-rep-remove" aria-label="<?php esc_attr_e( 'Remove row', 'datastream-api-sync' ); ?>">&times;</button>
						</p>
					</template>
					<p><button type="button" class="button ds-rep-add"><?php esc_html_e( 'Add taxonomy mapping', 'datastream-api-sync' ); ?></button></p>
					<input type="hidden" id="ds-taxonomy-json" name="stream[taxonomy_map]" value="<?php echo esc_attr( (string) $ds_stream['taxonomy_map'] ); ?>" />
				</fieldset>

				<fieldset class="ds-repeater" data-ds-repeater="meta" data-ds-target="ds-meta-json">
					<legend><?php esc_html_e( 'Custom fields (meta / ACF)', 'datastream-api-sync' ); ?></legend>
					<p class="description"><?php esc_html_e( 'Put the value of a field into any custom field, by name. ACF fields work by their field name.', 'datastream-api-sync' ); ?></p>
					<div class="ds-repeater-rows">
						<?php foreach ( $ds_meta_map as $ds_meta => $ds_field ) : ?>
							<p class="ds-repeater-row">
								<input type="text" class="ds-rep-key regular-text" value="<?php echo esc_attr( $ds_meta ); ?>" aria-label="<?php esc_attr_e( 'Custom field name', 'datastream-api-sync' ); ?>" placeholder="acf_price" />
								<span class="ds-rep-arrow" aria-hidden="true">←</span>
								<input type="text" class="ds-rep-value regular-text" value="<?php echo esc_attr( $ds_field ); ?>" aria-label="<?php esc_attr_e( 'Field name', 'datastream-api-sync' ); ?>" placeholder="price" />
								<button type="button" class="button-link ds-rep-remove" aria-label="<?php esc_attr_e( 'Remove row', 'datastream-api-sync' ); ?>">&times;</button>
							</p>
						<?php endforeach; ?>
					</div>
					<template class="ds-repeater-template">
						<p class="ds-repeater-row">
							<input type="text" class="ds-rep-key regular-text" value="" aria-label="<?php esc_attr_e( 'Custom field name', 'datastream-api-sync' ); ?>" placeholder="acf_price" />
							<span class="ds-rep-arrow" aria-hidden="true">←</span>
							<input type="text" class="ds-rep-value regular-text" value="" aria-label="<?php esc_attr_e( 'Field name', 'datastream-api-sync' ); ?>" placeholder="price" />
							<button type="button" class="button-link ds-rep-remove" aria-label="<?php esc_attr_e( 'Remove row', 'datastream-api-sync' ); ?>">&times;</button>
						</p>
					</template>
					<p><button type="button" class="button ds-rep-add"><?php esc_html_e( 'Add custom field mapping', 'datastream-api-sync' ); ?></button></p>
					<input type="hidden" id="ds-meta-json" name="stream[meta_map]" value="<?php echo esc_attr( (string) $ds_stream['meta_map'] ); ?>" />
				</fieldset>

				<?php if ( class_exists( 'WooCommerce' ) || post_type_exists( 'product' ) ) : ?>
					<div data-ds-show-woo <?php echo 'product' !== $ds_stream['post_type'] ? 'hidden' : ''; ?>>
						<p>
							<strong><?php esc_html_e( 'WooCommerce', 'datastream-api-sync' ); ?></strong><br />
							<label for="ds-price-field"><?php esc_html_e( 'Price from field', 'datastream-api-sync' ); ?></label>
							<input type="text" id="ds-price-field" name="stream[price_field]" class="small-text" style="width:10em" value="<?php echo esc_attr( $ds_stream['price_field'] ); ?>" placeholder="price" />
							<label for="ds-sku-field"><?php esc_html_e( 'SKU from field', 'datastream-api-sync' ); ?></label>
							<input type="text" id="ds-sku-field" name="stream[sku_field]" class="small-text" style="width:10em" value="<?php echo esc_attr( $ds_stream['sku_field'] ); ?>" placeholder="sku" />
							<label for="ds-stock-field"><?php esc_html_e( 'Stock from field', 'datastream-api-sync' ); ?></label>
							<input type="text" id="ds-stock-field" name="stream[stock_field]" class="small-text" style="width:10em" value="<?php echo esc_attr( $ds_stream['stock_field'] ); ?>" placeholder="stock" />
						</p>
					</div>
				<?php endif; ?>

				<p>
					<label for="ds-stale"><?php esc_html_e( 'When records disappear from the API', 'datastream-api-sync' ); ?></label>
					<select id="ds-stale" name="stream[stale_post_action]">
						<option value="keep" <?php selected( $ds_stream['stale_post_action'], 'keep' ); ?>><?php esc_html_e( 'Keep their posts', 'datastream-api-sync' ); ?></option>
						<option value="draft" <?php selected( $ds_stream['stale_post_action'], 'draft' ); ?>><?php esc_html_e( 'Move their posts to draft', 'datastream-api-sync' ); ?></option>
						<option value="trash" <?php selected( $ds_stream['stale_post_action'], 'trash' ); ?>><?php esc_html_e( 'Trash their posts', 'datastream-api-sync' ); ?></option>
					</select>
					<span class="description"><?php esc_html_e( 'Uses the auto-purge age from Sync settings, so a partial sync never removes content by mistake.', 'datastream-api-sync' ); ?></span>
				</p>
			</div>
		</div>
	</details>
	<?php else : ?>
	<details class="ds-card">
		<summary><?php esc_html_e( 'Turn records into posts', 'datastream-api-sync' ); ?> <span class="ds-card-hint"><?php esc_html_e( 'Pro', 'datastream-api-sync' ); ?></span></summary>
		<div class="ds-card-body">
			<p class="description"><?php esc_html_e( 'With Pro, each record becomes a real post, page, or WooCommerce product — with categories, custom fields, and images downloaded into your Media Library.', 'datastream-api-sync' ); ?></p>
		</div>
	</details>
	<?php endif; ?>

	<?php submit_button( $ds_is_new ? __( 'Add Stream', 'datastream-api-sync' ) : __( 'Save Stream', 'datastream-api-sync' ) ); ?>
</form>
