"use strict";
/**
 * /api/projects/:projectId/tracks/* routes — track CRUD (read/delete only).
 *
 * Runtime (run/start/stop/status) removed in M0 cleanup.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildTracksRouter = buildTracksRouter;
const express_1 = require("express");
const config_1 = require("../config");
const authz_1 = require("../middleware/authz");
const store_1 = require("../tracks/store");
const logger_1 = require("../logger");
const log = (0, logger_1.modLogger)('tracks-route');
function buildTracksRouter() {
    const router = (0, express_1.Router)();
    // GET /api/projects/:projectId/tracks
    router.get('/:projectId/tracks', (0, authz_1.requireProjectOwner)('projectId'), (req, res) => {
        const project = (0, config_1.getProject)(req.params.projectId);
        if (!project) {
            res.status(404).json({ error: 'Project not found' });
            return;
        }
        res.json({ files: (0, store_1.listTracks)(project.folderPath) });
    });
    // GET /api/projects/:projectId/tracks/file/:filename → { source }
    router.get('/:projectId/tracks/file/:filename', (0, authz_1.requireProjectOwner)('projectId'), (req, res) => {
        const project = (0, config_1.getProject)(req.params.projectId);
        if (!project) {
            res.status(404).json({ error: 'Project not found' });
            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.loadTrack)(project.folderPath, safe);
        if (source === null) {
            res.status(404).json({ error: 'Track not found' });
            return;
        }
        const sidecar = (0, store_1.loadSidecar)(project.folderPath, safe);
        res.json({ filename: safe, source, sidecar });
    });
    // DELETE /api/projects/:projectId/tracks/file/:filename
    router.delete('/:projectId/tracks/file/:filename', (0, authz_1.requireProjectOwner)('projectId'), (req, res) => {
        const project = (0, config_1.getProject)(req.params.projectId);
        if (!project) {
            res.status(404).json({ error: 'Project not found' });
            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.deleteTrack)(project.folderPath, safe);
        log.info({ projectId: project.id, filename: safe, ok }, 'track deleted');
        res.json({ ok });
    });
    return router;
}
//# sourceMappingURL=tracks.js.map