<?php
/**
 * API Response Handler
 * Standardized JSON responses for API endpoints
 */

class ApiResponse {
    /**
     * Send success response
     */
    public function success($data = null, $message = 'Success', $code = 200) {
        http_response_code($code);
        echo json_encode([
            'success' => true,
            'message' => $message,
            'data' => $data,
            'timestamp' => date('c')
        ], JSON_PRETTY_PRINT);
        exit();
    }
    
    /**
     * Send error response
     */
    public function error($message = 'Error', $code = 400, $errors = null) {
        http_response_code($code);
        echo json_encode([
            'success' => false,
            'message' => $message,
            'errors' => $errors,
            'timestamp' => date('c')
        ], JSON_PRETTY_PRINT);
        exit();
    }
    
    /**
     * Send paginated response
     */
    public function paginated($data, $page, $per_page, $total, $message = 'Success') {
        http_response_code(200);
        echo json_encode([
            'success' => true,
            'message' => $message,
            'data' => $data,
            'pagination' => [
                'page' => (int)$page,
                'per_page' => (int)$per_page,
                'total' => (int)$total,
                'total_pages' => ceil($total / $per_page)
            ],
            'timestamp' => date('c')
        ], JSON_PRETTY_PRINT);
        exit();
    }
}
