<?php
/**
 * REST API — Author Performance (newsroom-os/v1).
 *
 * @package Newsroom_AI_Assistant
 */

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

add_action( 'rest_api_init', 'newsai_register_author_perf_rest_routes' );

if ( ! function_exists( 'newsai_register_author_perf_rest_routes' ) ) {
	function newsai_register_author_perf_rest_routes() {
		register_rest_route(
			'newsroom-os/v1',
			'/author-performance',
			[
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => 'newsai_rest_get_author_perf',
				'permission_callback' => 'newsai_rest_author_perf_can_view',
				'args'                => [
					'days' => [
						'type'              => 'integer',
						'default'           => 30,
						'sanitize_callback' => static function ( $v ) {
							$v = (int) $v;
							return in_array( $v, [ 7, 30, 90 ], true ) ? $v : 30;
						},
					],
				],
			]
		);
	}
}

if ( ! function_exists( 'newsai_rest_author_perf_can_view' ) ) {
	function newsai_rest_author_perf_can_view() {
		return function_exists( 'newsai_author_perf_can_access' )
			? newsai_author_perf_can_access()
			: current_user_can( 'edit_others_posts' );
	}
}

if ( ! function_exists( 'newsai_rest_get_author_perf' ) ) {
	function newsai_rest_get_author_perf( WP_REST_Request $request ) {
		if ( ! function_exists( 'newsai_author_perf_registry_payload' ) ) {
			return new WP_REST_Response( [ 'error' => 'registry_missing' ], 500 );
		}
		$days = (int) $request->get_param( 'days' );
		try {
			return new WP_REST_Response( newsai_author_perf_registry_payload( $days ), 200 );
		} catch ( \Throwable $e ) {
			return new WP_REST_Response(
				[
					'code'    => 'payload_error',
					'message' => $e->getMessage(),
				],
				500
			);
		}
	}
}

add_action( 'wp_ajax_newsai_author_perf_registry_get', 'newsai_ajax_author_perf_registry_get' );

if ( ! function_exists( 'newsai_ajax_author_perf_registry_get' ) ) {
	function newsai_ajax_author_perf_registry_get() {
		check_ajax_referer( 'newsai_ajax_nonce', 'nonce' );
		if ( ! function_exists( 'newsai_author_perf_can_access' ) || ! newsai_author_perf_can_access() ) {
			wp_send_json_error( [ 'message' => __( 'Forbidden.', 'newsroom-ai-assistant' ) ], 403 );
		}
		$days = isset( $_POST['days'] ) ? (int) wp_unslash( $_POST['days'] ) : 30;
		if ( ! function_exists( 'newsai_author_perf_registry_payload' ) ) {
			wp_send_json_error( [ 'message' => 'registry_missing' ], 500 );
		}
		try {
			wp_send_json_success( newsai_author_perf_registry_payload( $days ) );
		} catch ( \Throwable $e ) {
			wp_send_json_error( [ 'message' => $e->getMessage() ], 500 );
		}
	}
}

add_action( 'wp_ajax_newsai_author_perf_registry_expanded', 'newsai_ajax_author_perf_registry_expanded' );

if ( ! function_exists( 'newsai_ajax_author_perf_registry_expanded' ) ) {
	function newsai_ajax_author_perf_registry_expanded() {
		check_ajax_referer( 'newsai_pro_author_perf', 'nonce' );
		if ( ! current_user_can( 'edit_others_posts' ) ) {
			wp_send_json_error( [ 'message' => __( 'Forbidden.', 'newsroom-ai-assistant' ) ], 403 );
		}
		$user_id = isset( $_POST['user_id'] ) ? (int) wp_unslash( $_POST['user_id'] ) : 0;
		$days    = isset( $_POST['days'] ) ? (int) wp_unslash( $_POST['days'] ) : 30;
		if ( ! function_exists( 'newsai_author_perf_registry_expanded' ) ) {
			wp_send_json_error( [ 'message' => 'registry_missing' ], 500 );
		}
		$data = newsai_author_perf_registry_expanded( $user_id, $days );
		if ( ! $data ) {
			wp_send_json_error( [ 'message' => __( 'No data found.', 'newsroom-ai-assistant' ) ] );
		}
		wp_send_json_success( $data );
	}
}
