<?php
declare(strict_types=1);

$files = [
	'old' => __DIR__ . '/storage/backups/db/hosting-export-gis100-20260720-161810.sql',
	'new' => __DIR__ . '/storage/backups/db/hosting-export-gis100-20260720-170149.sql',
];

foreach ( $files as $label => $f ) {
	if ( ! is_file( $f ) ) {
		echo "missing {$label}\n";
		continue;
	}
	$h = fopen( $f, 'r' );
	$tables = [];
	while ( ( $line = fgets( $h ) ) !== false ) {
		if ( preg_match( '/^CREATE TABLE `([^`]+)`/', $line, $m ) ) {
			$tables[] = $m[1];
		}
	}
	fclose( $h );
	$i = array_search( 'wp_ai_acc_flows', $tables, true );
	echo "=== {$label} total=" . count( $tables ) . " flows_idx=" . ( $i === false ? 'n/a' : (string) $i ) . " ===\n";
	if ( $i === false ) {
		continue;
	}
	file_put_contents(
		__DIR__ . "/storage/backups/db/tables-before-flows-{$label}.txt",
		implode( "\n", array_slice( $tables, 0, (int) $i ) ) . "\n"
	);
	file_put_contents(
		__DIR__ . "/storage/backups/db/tables-from-flows-{$label}.txt",
		implode( "\n", array_slice( $tables, (int) $i ) ) . "\n"
	);
	echo "before=" . $i . " from=" . ( count( $tables ) - (int) $i ) . "\n";
}
