<?php
/**
 * Diagnostics module.
 *
 * @package MonitorCat\Modules\Diagnostics
 */

declare(strict_types=1);

namespace MonitorCat\Modules\Diagnostics;

defined( 'ABSPATH' ) || exit;

use MonitorCat\Module\AbstractModule;

/**
 * Diagnostics module for system health checks and environment information.
 */
final class DiagnosticsModule extends AbstractModule {

	/**
	 * Get the module slug.
	 *
	 * @return string
	 */
	public function slug(): string {
		return 'diagnostics';
	}

	/**
	 * Get the module name.
	 *
	 * @return string
	 */
	public function name(): string {
		return __( 'Diagnostics', 'monitor-cat' );
	}

	/**
	 * Get the FontAwesome icon class.
	 *
	 * @return string
	 */
	public function icon(): string {
		return 'fa-stethoscope';
	}

	/**
	 * Get the module description.
	 *
	 * @return string
	 */
	public function description(): string {
		return __( 'System health checks and environment information', 'monitor-cat' );
	}

	/**
	 * Register REST API routes for the Diagnostics module.
	 */
	public function register_routes(): void {
		$controller = new RestController();
		$controller->register();
	}

	/**
	 * Get dashboard summary data.
	 *
	 * Uses the pre-computed summary from the enriched response format.
	 *
	 * @return array<string, mixed>
	 */
	public function get_dashboard_summary(): array {
		$cached = get_transient( 'monitorcat_diag_info' );

		if ( false !== $cached && is_array( $cached ) && isset( $cached['summary'] ) ) {
			$summary = $cached['summary'];
			$issues  = $summary['warnings'] + $summary['critical'];

			return array(
				'status' => 0 === $issues ? 'good' : ( $summary['critical'] > 0 ? 'critical' : 'warning' ),
				'issues' => $issues,
				'label'  => 0 === $issues
					? __( 'All checks passed', 'monitor-cat' )
					: sprintf(
						/* translators: %d: number of issues */
						_n( '%d issue found', '%d issues found', $issues, 'monitor-cat' ),
						$issues
					),
			);
		}

		return array(
			'status' => 'ready',
			'label'  => __( 'Run diagnostics to check system health', 'monitor-cat' ),
		);
	}
}
