<?php
declare(strict_types=1);

header('Content-Type: application/json; charset=utf-8');

$printerId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($printerId <= 0) {
    http_response_code(400);
    echo json_encode(['error' => 'printer id missing'], JSON_UNESCAPED_UNICODE);
    exit;
}

$app = app();
$pdo = $app->pdo();

if (!$pdo) {
    http_response_code(500);
    echo json_encode(['error' => 'DB disabled or unavailable'], JSON_UNESCAPED_UNICODE);
    exit;
}

$repo = new \App\Repository\MaterialMatrixRepository($pdo);
$printer = $repo->getPrinterById($printerId);

if (!$printer) {
    http_response_code(404);
    echo json_encode(['error' => 'printer not found'], JSON_UNESCAPED_UNICODE);
    exit;
}

$materials = $repo->listMaterialsForPrinter($printerId);

echo json_encode([
    'printer' => $printer,
    'materials' => $materials,
], JSON_UNESCAPED_UNICODE);
exit;
