<?php
/**
 * Activity Log REST API Controller
 * 
 * @package WooNooW\Api
 */

namespace WooNooW\Api;

use WooNooW\Core\ActivityLog\Logger;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

class ActivityLogController {
	
	/**
	 * REST API namespace
	 */
	private $namespace = 'woonoow/v1';
	
	/**
	 * REST API base
	 */
	private $rest_base = 'activity-log';
	
	/**
	 * Register routes
	 */
	public function register_routes() {
		// GET /woonoow/v1/activity-log
		register_rest_route($this->namespace, '/' . $this->rest_base, [
			[
				'methods' => 'GET',
				'callback' => [$this, 'get_activities'],
				'permission_callback' => [$this, 'check_permission'],
			],
		]);
		
		// POST /woonoow/v1/activity-log
		register_rest_route($this->namespace, '/' . $this->rest_base, [
			[
				'methods' => 'POST',
				'callback' => [$this, 'create_activity'],
				'permission_callback' => [$this, 'check_permission'],
			],
		]);
		
		// GET /woonoow/v1/activity-log/stats
		register_rest_route($this->namespace, '/' . $this->rest_base . '/stats', [
			[
				'methods' => 'GET',
				'callback' => [$this, 'get_stats'],
				'permission_callback' => [$this, 'check_permission'],
			],
		]);
	}
	
	/**
	 * Get activities
	 * 
	 * @param WP_REST_Request $request
	 * @return WP_REST_Response
	 */
	public function get_activities(WP_REST_Request $request) {
		$args = [
			'page' => $request->get_param('page') ?: 1,
			'per_page' => $request->get_param('per_page') ?: 20,
			'action' => $request->get_param('action'),
			'object_type' => $request->get_param('object_type'),
			'object_id' => $request->get_param('object_id'),
			'user_id' => $request->get_param('user_id'),
			'date_from' => $request->get_param('date_from'),
			'date_to' => $request->get_param('date_to'),
			'search' => $request->get_param('search'),
		];
		
		$result = Logger::get_activities($args);
		
		return new WP_REST_Response($result, 200);
	}
	
	/**
	 * Create activity
	 * 
	 * @param WP_REST_Request $request
	 * @return WP_REST_Response|WP_Error
	 */
	public function create_activity(WP_REST_Request $request) {
		$params = $request->get_json_params();
		
		$action = isset($params['action']) ? $params['action'] : null;
		$object_type = isset($params['object_type']) ? $params['object_type'] : null;
		$object_id = isset($params['object_id']) ? $params['object_id'] : null;
		$description = isset($params['description']) ? $params['description'] : '';
		$metadata = isset($params['metadata']) ? $params['metadata'] : [];
		
		if (empty($action) || empty($object_type) || empty($object_id)) {
			return new WP_Error(
				'invalid_params',
				__('Action, object_type, and object_id are required', 'woonoow'),
				['status' => 400]
			);
		}
		
		$activity_id = Logger::log($action, $object_type, $object_id, $description, $metadata);
		
		if ($activity_id === false) {
			return new WP_Error(
				'log_failed',
				__('Failed to log activity', 'woonoow'),
				['status' => 500]
			);
		}
		
		return new WP_REST_Response([
			'success' => true,
			'activity_id' => $activity_id,
		], 201);
	}
	
	/**
	 * Get activity stats
	 * 
	 * @param WP_REST_Request $request
	 * @return WP_REST_Response
	 */
	public function get_stats(WP_REST_Request $request) {
		$date_from = $request->get_param('date_from');
		$date_to = $request->get_param('date_to');
		
		$stats = Logger::get_stats($date_from, $date_to);
		
		return new WP_REST_Response($stats, 200);
	}
	
	/**
	 * Check permission
	 * 
	 * @return bool
	 */
	public function check_permission() {
		return current_user_can('manage_woocommerce') || current_user_can('manage_options');
	}
}
