"use strict";
/**
 * /api/global/tracks/* routes — per-user global track templates (read/delete only).
 *
 * Write (PUT) removed in M0 cleanup.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const store_1 = require("../tracks/store");
const logger_1 = require("../logger");
const log = (0, logger_1.modLogger)('global-tracks-route');
const router = (0, express_1.Router)();
function requireUser(req, res) {
    const username = req.user?.username;
    if (!username) {
        res.status(401).json({ error: 'auth required' });
        return null;
    }
    return username;
}
// GET /api/global/tracks
router.get('/', (req, res) => {
    const username = requireUser(req, res);
    if (!username)
        return;
    res.json({ files: (0, store_1.listGlobalTracks)(username) });
});
// GET /api/global/tracks/file/:filename
router.get('/file/:filename', (req, res) => {
    const username = requireUser(req, res);
    if (!username)
        return;
    const safe = (0, store_1.sanitizeTrackFilename)(req.params.filename);
    if (!safe) {
        res.status(400).json({ error: 'invalid filename' });
        return;
    }
    const source = (0, store_1.loadGlobalTrack)(username, safe);
    if (source === null) {
        res.status(404).json({ error: 'Global track not found' });
        return;
    }
    res.json({ filename: safe, source });
});
// DELETE /api/global/tracks/file/:filename
router.delete('/file/:filename', (req, res) => {
    const username = requireUser(req, res);
    if (!username)
        return;
    const safe = (0, store_1.sanitizeTrackFilename)(req.params.filename);
    if (!safe) {
        res.status(400).json({ error: 'invalid filename' });
        return;
    }
    const ok = (0, store_1.deleteGlobalTrack)(username, safe);
    log.info({ username, filename: safe, ok }, 'global track deleted');
    res.json({ ok });
});
exports.default = router;
//# sourceMappingURL=global-tracks.js.map