/**
 * Upgrade Pilot admin: scan progress driver.
 *
 * While this page is open, the browser drives queue ticks via admin-ajax —
 * the scan progresses even on hosts with broken WP-Cron. Cron remains the
 * background fallback when nobody is watching.
 */
( function () {
	'use strict';

	var cfg = window.upilotAdmin || {};

	function post( action, cb ) {
		var body = new window.FormData();
		body.append( 'action', action );
		body.append( 'nonce', cfg.nonce );
		window
			.fetch( cfg.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: body } )
			.then( function ( r ) {
				return r.json();
			} )
			.then( function ( json ) {
				cb( null, json );
			} )
			.catch( function ( err ) {
				cb( err );
			} );
	}

	function setProgressText( text ) {
		var el = document.getElementById( 'upilot-progress-text' );
		if ( el ) {
			el.textContent = text;
		}
	}

	function terminalFailure() {
		setProgressText( cfg.i18n.terminal );
		var spinner = document.querySelector( '#upilot-progress .spinner' );
		if ( spinner ) {
			spinner.classList.remove( 'is-active' );
		}
		var btn = document.getElementById( 'upilot-start-scan' );
		if ( btn ) {
			btn.disabled = false;
			btn.textContent = cfg.i18n.rescan;
		}
	}

	function tickLoop( failures ) {
		post( 'upilot_tick', function ( err, json ) {
			if ( err || ! json || ! json.success ) {
				failures = ( failures || 0 ) + 1;
				if ( failures > 5 ) {
					terminalFailure();
					return;
				}
				setProgressText( cfg.i18n.failed );
				window.setTimeout( function () {
					tickLoop( failures );
				}, 3000 );
				return;
			}
			var s = json.data || {};
			if ( s.status === 'running' ) {
				setProgressText( ( s.done || 0 ) + ' / ' + ( s.total || 0 ) + ' ' + cfg.i18n.files );
				window.setTimeout( function () {
					tickLoop( 0 );
				}, 400 );
			} else {
				setProgressText( cfg.i18n.done );
				window.location.reload();
			}
		} );
	}

	function startScan() {
		var btn = document.getElementById( 'upilot-start-scan' );
		var progress = document.getElementById( 'upilot-progress' );
		if ( btn ) {
			btn.disabled = true;
			btn.textContent = cfg.i18n.scanning;
		}
		if ( progress ) {
			progress.hidden = false;
		}
		post( 'upilot_start_scan', function ( err, json ) {
			if ( err || ! json || ! json.success ) {
				setProgressText( cfg.i18n.failed );
				return;
			}
			tickLoop( 0 );
		} );
	}

	document.addEventListener( 'DOMContentLoaded', function () {
		var startBtn = document.getElementById( 'upilot-start-scan' );
		if ( startBtn ) {
			startBtn.addEventListener( 'click', startScan );
			// If a scan is already running when the page opens, resume driving it.
			var progress = document.getElementById( 'upilot-progress' );
			if ( progress && ! progress.hidden ) {
				tickLoop( 0 );
			}
		}

		var snapBtn = document.getElementById( 'upilot-run-snapshot' );
		if ( snapBtn ) {
			snapBtn.addEventListener( 'click', function () {
				snapBtn.disabled = true;
				var sp = document.getElementById( 'upilot-snap-progress' );
				if ( sp ) {
					sp.hidden = false;
				}
				post( 'upilot_run_snapshot', function ( err, json ) {
					if ( err || ! json || ! json.success ) {
						snapBtn.disabled = false;
						if ( sp ) {
							sp.hidden = true;
						}
						window.alert( cfg.i18n.snapFailed );
						return;
					}
					window.location.reload();
				} );
			} );
		}
	} );
} )();
