<?php

declare(strict_types=1);

namespace FormConnector\API\Endpoints;

use FormConnector\Features\License\License;
use FormConnector\Integration\Health\HealthCheck;

/**
 * Health check REST API endpoints.
 */
class HealthEndpoints {

	private const NAMESPACE = 'fc/v1';

	/**
	 * Register health routes.
	 */
	public static function register(): void {
		register_rest_route(self::NAMESPACE, '/health', [
			'methods' => \WP_REST_Server::READABLE,
			'callback' => [self::class, 'get_status'],
			'permission_callback' => [self::class, 'permission_check'],
		]);

		register_rest_route(self::NAMESPACE, '/health/(?P<check>system|api|forms|database)', [
			'methods' => \WP_REST_Server::READABLE,
			'callback' => [self::class, 'get_check'],
			'permission_callback' => [self::class, 'permission_check'],
			'args' => [
				'check' => [
					'required' => true,
					'type' => 'string',
					'enum' => ['system', 'api', 'forms', 'database'],
				],
			],
		]);

		register_rest_route(self::NAMESPACE, '/dashboard', [
			'methods' => \WP_REST_Server::READABLE,
			'callback' => [self::class, 'get_dashboard'],
			'permission_callback' => [self::class, 'permission_check'],
		]);
	}

	/**
	 * Permission check for admin-only endpoints.
	 */
	public static function permission_check(): bool {
		return current_user_can('manage_options');
	}

	/**
	 * Get full health status.
	 */
	public static function get_status(\WP_REST_Request $request): \WP_REST_Response {
		$health = new HealthCheck();
		
		return new \WP_REST_Response([
			'success' => true,
			'data' => $health->run_all(),
		], 200);
	}

	/**
	 * Get specific health check.
	 */
	public static function get_check(\WP_REST_Request $request): \WP_REST_Response {
		$check_name = $request->get_param('check');
		$health = new HealthCheck();

		$method = 'check_' . $check_name;
		
		if (!method_exists($health, $method)) {
			return new \WP_REST_Response([
				'success' => false,
				'message' => __('Invalid health check type.', 'form-connector'),
			], 400);
		}

		return new \WP_REST_Response([
			'success' => true,
			'data' => $health->$method(),
		], 200);
	}

	/**
	 * Get dashboard summary data.
	 */
	public static function get_dashboard(\WP_REST_Request $request): \WP_REST_Response {
		$license = new License();
		$health = new HealthCheck();

		// Get actual form mappings count
		$mappings = new \FormConnector\Integration\Forms\Mappings();
		$all_mappings = $mappings->get_all_mappings();
		$form_count = 0;
		foreach ($all_mappings as $plugin_mappings) {
			if (is_array($plugin_mappings)) {
				$form_count += count($plugin_mappings);
			}
		}

		// Get actual submission stats from logs
		$logs = \FormConnector\Features\Logs\Logs::get_instance();
		$stats = $logs->get_stats();

		return new \WP_REST_Response([
			'success' => true,
			'data' => [
				'license' => $license->get_status(),
				'health' => $health->run_all(),
				'stats' => [
					'form_mappings' => $form_count,
					'total_submissions' => $stats['total'] ?? 0,
					'successful_submissions' => $stats['success'] ?? 0,
					'failed_submissions' => $stats['error'] ?? 0,
					'today' => $stats['today'] ?? 0,
					'this_week' => $stats['this_week'] ?? 0,
				],
				'plugin' => [
					'version' => defined('FC_VERSION') ? FC_VERSION : '3.0.0',
					'php_version' => phpversion(),
					'wp_version' => get_bloginfo('version'),
				],
			],
		], 200);
	}
}
