<?php
header("Content-Type: application/json");
require_once __DIR__ . "/../auth/require-auth.php";
require_once __DIR__ . "/../services/db.php";

$conn = db();
$projectId = isset($_GET['project_id']) ? (int)$_GET['project_id'] : null;

$sql = "SELECT id, name, color FROM tags " . ($projectId ? "WHERE project_id = ?" : "");
$params = $projectId ? [$projectId] : [];

$stmt = sqlsrv_query($conn, $sql, $params);

$tags = [];
if ($stmt !== false) {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        $tags[] = [
            "id" => (int)$row['id'],
            "name" => $row['name'],
            "color" => $row['color']
        ];
    }
}

echo json_encode($tags);