<?php
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/controllers/AuthController.php';

function sendSuccess($data = null, $message = '') {
    echo json_encode(['success' => true, 'data' => $data, 'message' => $message]);
    exit;
}

function sendError($message, $code = 500, $data = null) {
    echo json_encode(['success' => false, 'error' => $message, 'code' => $code, 'data' => $data]);
    exit;
}

function getJsonBody() {
    return [
        'name' => 'Test User ' . time(),
        'email' => 'test' . time() . '@example.com',
        'password' => 'password123',
        'phone' => '1234567890',
        'address' => '123 Test St, Test City'
    ];
}

ob_start();
try {
    AuthController::register();
} catch (Exception $e) {}
$output = ob_get_clean();

$data = json_decode($output, true);
if ($data && isset($data['data']['user']['address']) && $data['data']['user']['address'] === '123 Test St, Test City') {
    echo "SUCCESS: Address saved and returned in registration response.\n";
    
    // Cleanup the test user
    $db = getDb();
    $userId = $data['data']['user']['id'];
    $db->query("DELETE FROM users WHERE id = $userId");
    echo "Cleanup: Test user deleted.\n";
} else {
    echo "FAILURE: Address NOT saved or returned correctly.\n";
    echo "Response: " . $output . "\n";
}
