<?php
require 'db.php';
header('Content-Type: application/json');

$id = $_GET['id'] ?? null;
if (!$id) exit;

$stmt = $pdo->prepare("SELECT name FROM departments WHERE id = ?");
$stmt->execute([$id]);
$deptName = $stmt->fetchColumn();

$countStmt = $pdo->prepare(
    "SELECT COUNT(*) FROM transactions WHERE department = ?"
);
$countStmt->execute([$deptName]);
$used = $countStmt->fetchColumn() > 0;

$allDepts = $pdo->query(
    "SELECT name FROM departments ORDER BY name"
)->fetchAll(PDO::FETCH_COLUMN);

echo json_encode([
    'in_use' => $used,
    'departments' => $allDepts
]);
