<?php

if ( ! defined( 'ABSPATH' ) ) { exit; }
?>
<div class="aieo-dmm-section aieo-remote-export">
	<h2><?php esc_html_e( 'Insights Remote DB Settings', 'ai-eshop-optimizer' ); ?></h2>
	<p class="description">
		<?php esc_html_e( 'Stream AIEO raw events + rollups to an operator-owned remote MariaDB / MySQL instance for unbounded BI / analyst access. The local store stays small (capped by retention); the remote holds the full event history.', 'ai-eshop-optimizer' ); ?>
	</p>
	<p class="description">
		<?php
		echo wp_kses_post( sprintf(
			
			__( 'Manage destination connections at %s. They are the same encrypted connections used by the ERP-bridge tab; passwords never round-trip through this form.', 'ai-eshop-optimizer' ),
			'<a href="' . esc_url( admin_url( 'admin.php?page=' . AIEO_DMM::MENU_SLUG . '&dmm_tab=erp_database' ) ) . '">' . esc_html__( 'Tools → ERP Database', 'ai-eshop-optimizer' ) . '</a>'
		) );
		?>
	</p>

	<?php if ( empty( $connections ) ) : ?>
		<div class="notice notice-warning inline">
			<p>
				<?php
				echo wp_kses_post( sprintf(
					
					__( 'No ERP connections configured yet. Add one in %s first.', 'ai-eshop-optimizer' ),
					'<a href="' . esc_url( admin_url( 'admin.php?page=' . AIEO_DMM::MENU_SLUG . '&dmm_tab=erp_database' ) ) . '">' . esc_html__( 'Tools → ERP Database', 'ai-eshop-optimizer' ) . '</a>'
				) );
				?>
			</p>
		</div>
	<?php endif; ?>

	<div class="aieo-dmm-card" id="aieo-remote-export-settings">
		<h3><?php esc_html_e( 'Settings', 'ai-eshop-optimizer' ); ?></h3>
		<form id="aieo-remote-export-form" method="post" autocomplete="off">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row"><label for="aieo-rex-connection"><?php esc_html_e( 'Destination connection', 'ai-eshop-optimizer' ); ?></label></th>
						<td>
							<select id="aieo-rex-connection" name="connection_id">
								<option value="0"><?php esc_html_e( '— select a connection —', 'ai-eshop-optimizer' ); ?></option>
								<?php foreach ( $connections as $c ) : ?>
									<option value="<?php echo esc_attr( (string) $c['id'] ); ?>" <?php selected( (int) $settings['connection_id'], (int) $c['id'] ); ?>>
										<?php echo esc_html( $c['name'] . ' (' . $c['host'] . ' / ' . $c['db_name'] . ')' ); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Tables to export', 'ai-eshop-optimizer' ); ?></th>
						<td>
							<?php foreach ( $tables as $slug => $label ) : ?>
								<label class="aieo-rex-table-row">
									<input type="checkbox" name="tables[]" value="<?php echo esc_attr( $slug ); ?>" <?php checked( in_array( $slug, (array) $settings['tables'], true ) ); ?>>
									<code><?php echo esc_html( $slug ); ?></code>
									<span class="description"><?php echo esc_html( $label ); ?></span>
								</label><br>
							<?php endforeach; ?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="aieo-rex-frequency"><?php esc_html_e( 'Export frequency', 'ai-eshop-optimizer' ); ?></label></th>
						<td>
							<select id="aieo-rex-frequency" name="frequency">
								<option value="15min"  <?php selected( $settings['frequency'], '15min' ); ?>><?php esc_html_e( 'Every 15 minutes', 'ai-eshop-optimizer' ); ?></option>
								<option value="hourly" <?php selected( $settings['frequency'], 'hourly' ); ?>><?php esc_html_e( 'Hourly', 'ai-eshop-optimizer' ); ?></option>
								<option value="daily"  <?php selected( $settings['frequency'], 'daily' ); ?>><?php esc_html_e( 'Daily', 'ai-eshop-optimizer' ); ?></option>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="aieo-rex-batch"><?php esc_html_e( 'Batch size', 'ai-eshop-optimizer' ); ?></label></th>
						<td>
							<input id="aieo-rex-batch" type="number" name="batch_size" min="50" max="50000" step="50" value="<?php echo esc_attr( (string) $settings['batch_size'] ); ?>">
							<p class="description"><?php esc_html_e( 'Rows per remote INSERT IGNORE statement. Lower if the remote times out on large batches.', 'ai-eshop-optimizer' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Local retention failsafe', 'ai-eshop-optimizer' ); ?></th>
						<td>
							<label>
								<input type="checkbox" name="purge_only_on_success" value="1" <?php checked( ! empty( $settings['purge_only_on_success'] ) ); ?>>
								<?php esc_html_e( 'Hold local archive deletes until the remote confirms (recommended).', 'ai-eshop-optimizer' ); ?>
							</label>
							<p class="description"><?php esc_html_e( 'When ON, the AE / Reco archive purges check the remote-export bookkeeping before deleting any hot row. If the remote is unreachable, local rows are kept until the export catches up.', 'ai-eshop-optimizer' ); ?></p>
						</td>
					</tr>
				</tbody>
			</table>
			<p>
				<button type="submit" class="button button-primary" id="aieo-rex-save"><?php esc_html_e( 'Save settings', 'ai-eshop-optimizer' ); ?></button>
				<button type="button" class="button" id="aieo-rex-force-run"><?php esc_html_e( 'Run export now', 'ai-eshop-optimizer' ); ?></button>
				<span class="aieo-rex-msg" id="aieo-rex-settings-msg"></span>
			</p>
		</form>
	</div>

	<div class="aieo-dmm-card" id="aieo-remote-export-dashboard">
		<h3><?php esc_html_e( 'Per-table status', 'ai-eshop-optimizer' ); ?></h3>
		<table class="widefat striped aieo-rex-dash">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Table', 'ai-eshop-optimizer' ); ?></th>
					<th><?php esc_html_e( 'Local rows', 'ai-eshop-optimizer' ); ?></th>
					<th><?php esc_html_e( 'Last exported ID', 'ai-eshop-optimizer' ); ?></th>
					<th><?php esc_html_e( 'Last exported ts', 'ai-eshop-optimizer' ); ?></th>
					<th><?php esc_html_e( 'Lag', 'ai-eshop-optimizer' ); ?></th>
					<th><?php esc_html_e( 'Last status', 'ai-eshop-optimizer' ); ?></th>
					<th><?php esc_html_e( 'Actions', 'ai-eshop-optimizer' ); ?></th>
				</tr>
			</thead>
			<tbody id="aieo-rex-dash-body">
				<?php foreach ( $dashboard as $row ) : ?>
					<tr data-slug="<?php echo esc_attr( $row['slug'] ); ?>">
						<td><code><?php echo esc_html( $row['slug'] ); ?></code><br><span class="description"><?php echo esc_html( $row['label'] ); ?></span></td>
						<td><?php echo esc_html( number_format_i18n( (int) $row['local_rows'] ) ); ?></td>
						<td><?php echo esc_html( number_format_i18n( (int) $row['last_exported_id'] ) ); ?></td>
						<td><?php echo esc_html( $row['last_exported_ts'] ? $row['last_exported_ts'] : '—' ); ?></td>
						<td>
							<?php
							if ( $row['lag_seconds'] === null ) {
								echo '—';
							} else {
								echo esc_html( human_time_diff( time() - (int) $row['lag_seconds'], time() ) );
							}
							?>
						</td>
						<td class="aieo-rex-status aieo-rex-status-<?php echo esc_attr( $row['last_status'] ?: 'none' ); ?>">
							<?php echo esc_html( $row['last_status'] ?: '—' ); ?>
							<?php if ( ! empty( $row['last_error'] ) ) : ?>
								<br><span class="aieo-rex-err" title="<?php echo esc_attr( $row['last_error'] ); ?>"><?php echo esc_html( wp_trim_words( $row['last_error'], 8 ) ); ?></span>
							<?php endif; ?>
						</td>
						<td>
							<button type="button" class="button button-small aieo-rex-test" data-slug="<?php echo esc_attr( $row['slug'] ); ?>"><?php esc_html_e( 'Test', 'ai-eshop-optimizer' ); ?></button>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

	<div class="aieo-dmm-card">
		<h3><?php esc_html_e( 'Force re-export', 'ai-eshop-optimizer' ); ?></h3>
		<p class="description"><?php esc_html_e( 'Operator escape hatch — rewinds the bookkeeping watermark for one table to a chosen timestamp so the next cron tick re-streams every row after that point. INSERT IGNORE on the remote prevents duplicates.', 'ai-eshop-optimizer' ); ?></p>
		<p>
			<label>
				<?php esc_html_e( 'Table:', 'ai-eshop-optimizer' ); ?>
				<select id="aieo-rex-reexp-table">
					<option value=""><?php esc_html_e( '— pick —', 'ai-eshop-optimizer' ); ?></option>
					<?php foreach ( $tables as $slug => $label ) : ?>
						<option value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_html( $slug ); ?></option>
					<?php endforeach; ?>
				</select>
			</label>
			<label>
				<?php esc_html_e( 'From (GMT):', 'ai-eshop-optimizer' ); ?>
				<input type="text" id="aieo-rex-reexp-ts" placeholder="YYYY-MM-DD HH:MM:SS" size="22">
			</label>
			<button type="button" class="button" id="aieo-rex-reexp-run"><?php esc_html_e( 'Reset watermark', 'ai-eshop-optimizer' ); ?></button>
			<span class="aieo-rex-msg" id="aieo-rex-reexp-msg"></span>
		</p>
	</div>
</div>
