<?php
// PUBLIC API: GET /api/get_upi.php?api_key=XXX
// Returns the UPI ID linked to this API key
require_once __DIR__ . '/../includes/functions.php';

$apiKey = $_GET['api_key'] ?? '';

if (!$apiKey) {
    jsonResponse(['status' => 'error', 'message' => 'Missing api_key'], 400);
}

$user = getUserByApiKey($apiKey);
if (!$user) {
    jsonResponse(['status' => 'error', 'message' => 'Invalid api_key'], 401);
}

if ($user['status'] === 'banned') {
    jsonResponse(['status' => 'error', 'message' => 'Account suspended'], 403);
}

$expiresAt = strtotime($user['plan_expires_at']);
if ($expiresAt < time()) {
    jsonResponse(['status' => 'error', 'message' => 'Plan expired'], 402);
}

$upi = $user['fampay_upi_id'] ?? '';
if (empty($upi)) {
    jsonResponse(['status' => 'error', 'message' => 'UPI ID not configured. Set it in your dashboard first.'], 400);
}

jsonResponse([
    'status' => 'success',
    'upi_id' => $upi,
    'name' => $user['name'] ?? '',
    'plan' => $user['plan_label'] ?? 'free',
    'gmail_connected' => !empty($user['gmail_connected'])
]);
