<?php
/**
 * POST /api/admin/vendors_toggle_featured.php
 * Admin-only. Body (JSON): { id, is_featured_vendor }
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';
require_once __DIR__ . '/../includes/auth.php';

only_post_allowed();
require_login();
require_role(['admin']);

$input = get_json_input();
$id = (int) ($input['id'] ?? 0);
if ($id <= 0) {
    send_error('A valid user id is required.', 422);
}

$pdo = Database::connect();
$stmt = $pdo->prepare("SELECT id FROM users WHERE id = ? AND account_type IN ('personal','business')");
$stmt->execute([$id]);
if (!$stmt->fetch()) {
    send_error('Vendor not found.', 404);
}

$isFeatured = !empty($input['is_featured_vendor']) ? 1 : 0;
$stmt = $pdo->prepare('UPDATE users SET is_featured_vendor = ? WHERE id = ?');
$stmt->execute([$isFeatured, $id]);

send_json(true, $isFeatured ? 'Vendor featured.' : 'Vendor unfeatured.');
