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

$conn = db();

$projectId = isset($_GET['id']) ? (int)$_GET['id'] : null;

if (!$projectId) {
    http_response_code(400);
    echo json_encode(["error" => "Project ID is required"]);
    exit;
}

$sql = "SELECT p.id, p.name, p.description, p.status, p.due_date, u.name AS owner 
        FROM projects p 
        LEFT JOIN users u ON p.owner_id = u.id 
        WHERE p.id = ?";

$stmt = sqlsrv_query($conn, $sql, [$projectId]);
$project = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);

if (!$project) {
    http_response_code(404);
    echo json_encode(["error" => "Project not found"]);
    exit;
}

echo json_encode(["success" => true, "project" => $project]);