<?php

namespace FleekDash\Libs\API;

/**
 * Class ResponseService
 *
 * Handles API response formatting.
 *
 * @package FleekDash\Libs\API
 */
class ResponseService
{
    /**
     * Return a success response
     *
     * @param mixed $data The response data
     * @param string|null $message Optional success message
     * @param int $status HTTP status code
     * @return \WP_REST_Response
     */
    public static function success($data = null, $message = null, $status = 200)
    {
        $response = [
            'success' => true,
            'data' => $data
        ];

        if ($message) {
            $response['message'] = $message;
        }

        return new \WP_REST_Response($response, $status);
    }

    /**
     * Return an error response
     *
     * @param string $message Error message
     * @param mixed $data Additional error data
     * @param int $status HTTP status code
     * @return \WP_REST_Response
     */
    public static function error($message, $data = null, $status = 400)
    {
        $response = [
            'success' => false,
            'message' => $message
        ];

        if ($data !== null) {
            $response['data'] = $data;
        }

        return new \WP_REST_Response($response, $status);
    }

    /**
     * Return a paginated response
     *
     * @param array $items The items for the current page
     * @param array $meta Pagination metadata
     * @param int $status HTTP status code
     * @return \WP_REST_Response
     */
    public static function paginate($items, $meta, $status = 200)
    {
        return new \WP_REST_Response([
            'success' => true,
            'data' => $items,
            'meta' => [
                'pagination' => $meta
            ]
        ], $status);
    }
}
