<?php

namespace Limb_Chatbot\Includes\Api\V1\Controllers;

use Limb_Chatbot\Includes\Services\Helper;
use Limb_Chatbot\Includes\Services\Notifications\Notification_Log_Repository;
use WP_REST_Server;

/**
 * Notification logs REST controller.
 *
 * Provides read-only access to notification delivery logs.
 *
 * @since 2.0.1
 */
class Notification_Logs_Controller extends Rest_Controller {

	/**
	 * Route base.
	 *
	 * @var string
	 */
	protected $rest_base = 'notification-logs';

	/**
	 * @var Notification_Log_Repository
	 */
	private Notification_Log_Repository $repository;

	public function __construct() {
		$this->repository = new Notification_Log_Repository();
	}

	/**
	 * Register routes.
	 *
	 * GET /notification-logs
	 *
	 * @return void
	 */
	public function register_routes(): void {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			[
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_items' ],
					'permission_callback' => [ $this, 'permission_callback' ],
					'args'                => $this->get_collection_params(),
				],
			]
		);
	}

	/**
	 * Collection params for pagination and sorting.
	 *
	 * @return array
	 */
	public function get_collection_params(): array {
		return array_merge( parent::get_collection_params(), [
			'include' => [
				'description'       => __( 'Include extra related data in each notification log item.', 'limb-chatbot' ),
				'type'              => 'array',
				'sanitize_callback' => function ( $value ) {
					if ( ! is_array( $value ) ) {
						return [];
					}

					return array_values( array_filter( array_map( 'sanitize_key', $value ) ) );
				},
			],
			'orderby' => [
				'description'       => __( 'Sort by field.', 'limb-chatbot' ),
				'type'              => 'string',
				'default'           => 'id',
				'enum'              => [ 'id', 'sent_at', 'created_at', 'status', 'type_key', 'channel' ],
				'validate_callback' => 'rest_validate_request_arg',
				'sanitize_callback' => 'sanitize_key',
			],
			'order'   => [
				'description'       => __( 'Sort order.', 'limb-chatbot' ),
				'type'              => 'string',
				'default'           => 'desc',
				'enum'              => [ 'asc', 'desc' ],
				'validate_callback' => 'rest_validate_request_arg',
				'sanitize_callback' => 'sanitize_key',
			],
		] );
	}

	/**
	 * Restrict logs endpoint to administrators.
	 *
	 * @param  mixed  $request
	 * @return bool
	 */
	public function permission_callback( $request ): bool {
		if ( ! parent::permission_callback( $request ) ) {
			return false;
		}

		return current_user_can( 'manage_options' );
	}

	/**
	 * GET /notification-logs
	 *
	 * @param  mixed  $request
	 * @return mixed
	 */
	public function get_items( $request ) {
		try {
			$collection = $this->repository->get_items( $request->get_query_params() );
			$collection = $this->set_pagination_params( $collection, $request );

			return rest_ensure_response( $collection );
		} catch ( \Exception $e ) {
			Helper::log( $e, __METHOD__ );

			return Helper::get_wp_error( $e );
		}
	}
}

