<?php
/**
 * Link Assassin Module.
 *
 * Bulk-removes broken outbound links from post content and Elementor data.
 *
 *   - Frontend JS sends an "initialize" AJAX call to get a job ID + total
 *     candidate post count.
 *   - JS then sends repeated "process_batch" AJAX calls, 25 posts at a time.
 *   - A visual progress bar reflects completion in real time.
 *   - Each modified post is backed up to a `_platform_helm_la_backup_{timestamp}`
 *     postmeta entry before changes are written.
 *   - A "Restore" tab lists all backups and can roll back individual posts.
 *
 * @package Platform\Helm\Modules\LinkAssassin
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\LinkAssassin;

use Platform\Helm\Contracts\ModuleInterface;
use Platform\Helm\Core\Assets;

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

final class LinkAssassinModule implements ModuleInterface {

	public const BATCH_SIZE       = 25;
	public const JOB_TRANSIENT    = 'platform_helm_la_job_';
	public const JOB_TTL_SECONDS  = 3600; // 1 hour
	public const BACKUP_META_PREFIX = '_platform_helm_la_backup_';

	private PostScanner $scanner;
	private BatchProcessor $batch_processor;
	private BackupManager $backups;
	private AjaxController $ajax;
	private AdminUI $ui;

	public function __construct( Assets $assets ) {
		$this->scanner         = new PostScanner();
		$this->backups         = new BackupManager();
		$this->batch_processor = new BatchProcessor( $this->backups );
		$this->ajax            = new AjaxController(
			$this->scanner,
			$this->batch_processor,
			$this->backups
		);
		$this->ui = new AdminUI( $assets, $this->backups );
	}

	public function id(): string {
		return 'link_assassin';
	}

	public function name(): string {
		return __( 'Link Assassin', 'platform-helm' );
	}

	public function description(): string {
		return __( 'Bulk-remove broken outbound links from post content and Elementor data. Processes 25 posts at a time via AJAX with a progress bar. Every post is backed up before changes.', 'platform-helm' );
	}

	public function default_enabled(): bool {
		return true;
	}

	public function boot(): void {
		$this->ui->register_hooks();
		$this->ajax->register_hooks();
	}
}
