<?php

defined( 'ABSPATH' ) || exit;

class ZACLS_REST_API {

	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
	}

	public function register_routes() {
		register_rest_route( 'zacls/v1', '/stats', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_stats' ),
			'permission_callback' => array( $this, 'check_permission' ),
		) );

		register_rest_route( 'zacls/v1', '/logs', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_logs' ),
			'permission_callback' => array( $this, 'check_permission' ),
			'args'                => array(
				'limit'  => array(
					'type'              => 'integer',
					'default'           => 50,
					'validate_callback' => array( $this, 'validate_limit' ),
				),
				'offset' => array(
					'type'              => 'integer',
					'default'           => 0,
					'validate_callback' => array( $this, 'validate_offset' ),
				),
				'status' => array(
					'type'              => 'string',
					'default'           => '',
					'sanitize_callback' => 'sanitize_key',
					'validate_callback' => array( $this, 'validate_status' ),
				),
			),
		) );

		register_rest_route( 'zacls/v1', '/blocked-ips', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_blocked_ips' ),
			'permission_callback' => array( $this, 'check_permission' ),
		) );

		register_rest_route( 'zacls/v1', '/analytics/login-activity', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_login_activity' ),
			'permission_callback' => array( $this, 'check_permission' ),
			'args'                => array(
				'days' => array(
					'type'              => 'integer',
					'default'           => 14,
					'validate_callback' => array( $this, 'validate_days' ),
				),
			),
		) );

		register_rest_route( 'zacls/v1', '/analytics/top-attackers', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_top_attackers' ),
			'permission_callback' => array( $this, 'check_permission' ),
		) );

		register_rest_route( 'zacls/v1', '/analytics/countries', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_country_stats' ),
			'permission_callback' => array( $this, 'check_permission' ),
		) );

		register_rest_route( 'zacls/v1', '/health', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_health' ),
			'permission_callback' => array( $this, 'check_permission' ),
		) );

		register_rest_route( 'zacls/v1', '/health/recommendations', array(
			'methods'             => 'GET',
			'callback'            => array( $this, 'get_recommendations' ),
			'permission_callback' => array( $this, 'check_permission' ),
		) );
	}

	/**
	 * Validate the pagination limit.
	 *
	 * @param mixed $value Limit value.
	 * @return bool
	 */
	public function validate_limit( $value ) {
		$limit = (int) $value;
		return $limit >= 1 && $limit <= 100;
	}

	/**
	 * Validate the pagination offset.
	 *
	 * @param mixed $value Offset value.
	 * @return bool
	 */
	public function validate_offset( $value ) {
		return (int) $value >= 0;
	}

	/**
	 * Validate a login status filter.
	 *
	 * @param mixed $value Status value.
	 * @return bool
	 */
	public function validate_status( $value ) {
		if ( '' === $value ) {
			return true;
		}
		return in_array( $value, array( 'failed', 'success', 'blocked' ), true );
	}

	/**
	 * Validate a days range.
	 *
	 * @param mixed $value Days value.
	 * @return bool
	 */
	public function validate_days( $value ) {
		$days = (int) $value;
		return $days >= 1 && $days <= 90;
	}

	public function check_permission() {
		return current_user_can( 'manage_options' );
	}

	public function get_stats() {
		$stats = ZACLS_Analytics::get_summary_stats( 1 );
		return new WP_REST_Response( $stats, 200 );
	}

	public function get_logs( $request ) {
		$limit  = $request->get_param( 'limit' );
		$offset = $request->get_param( 'offset' );
		$logs   = ZACLS_Login_Limiter::get_recent_attempts( $limit, $offset );
		return new WP_REST_Response( $logs, 200 );
	}

	public function get_blocked_ips() {
		$ips = ZACLS_IP_Blocker::get_blocked_ips();
		return new WP_REST_Response( $ips, 200 );
	}

	public function get_login_activity( $request ) {
		$days    = $request->get_param( 'days' );
		$activity = ZACLS_Analytics::get_login_activity( $days );
		return new WP_REST_Response( $activity, 200 );
	}

	public function get_top_attackers() {
		$attackers = ZACLS_Analytics::get_top_attackers();
		return new WP_REST_Response( $attackers, 200 );
	}

	public function get_country_stats() {
		$countries = ZACLS_Analytics::get_country_stats();
		return new WP_REST_Response( $countries, 200 );
	}

	public function get_health() {
		$health = ZACLS_Health::calculate_score();
		return new WP_REST_Response( $health, 200 );
	}

	public function get_recommendations() {
		$recs = ZACLS_Health::get_recommendations();
		return new WP_REST_Response( $recs, 200 );
	}
}
