<?php
/**
 * Manual admin autonomy diagnostic runners.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Admin_Autonomy_Manual_Diagnostic_Runner {
	/** @var object|null */
	private $chat_agent;

	public function __construct( $chat_agent = null ) {
		$this->chat_agent = $chat_agent;
	}

	public function run_workspace_heartbeat() {
		return $this->run_chat_agent_diagnostic(
			'run_workspace_heartbeat_now',
			'niwa_ai_admin_autonomy_last_manual_workspace_heartbeat'
		);
	}

	public function run_daily_report() {
		return $this->run_chat_agent_diagnostic(
			'run_daily_report_now',
			'niwa_ai_admin_autonomy_last_manual_daily_report'
		);
	}

	private function run_chat_agent_diagnostic( $method, $option_name ) {
		$method = (string) $method;
		$result = $this->chat_agent && method_exists( $this->chat_agent, $method )
			? $this->chat_agent->{$method}()
			: array( 'success' => false, 'sent' => false, 'reason' => 'no_runtime' );

		update_option( (string) $option_name, $result, false );

		return $result;
	}
}
