/**
 * Workspace API route handlers
 *
 * CRUD operations for workspace configuration (repos, sync mappings).
 * Used by the dashboard WorkspacePage.
 */
import { ConfigManager } from '../../../core/config/config-manager.js';
import { sendJson } from '../router.js';
function validateSyncStructure(sync) {
    if (sync == null)
        return null;
    if (typeof sync !== 'object')
        return 'sync must be an object';
    const s = sync;
    for (const key of ['github', 'jira', 'ado']) {
        if (s[key] != null && typeof s[key] !== 'object') {
            return `sync.${key} must be an object`;
        }
    }
    return null;
}
export function createWorkspaceRouteHandlers(projectRoot) {
    const configManager = new ConfigManager(projectRoot);
    return {
        /** GET /api/workspace — return workspace config */
        async getWorkspace(_req, res, _params) {
            const config = await configManager.read();
            const ws = config.workspace;
            sendJson(res, {
                name: ws?.name ?? '',
                rootRepo: ws?.rootRepo ?? null,
                repos: ws?.repos ?? [],
            });
        },
        /** PATCH /api/workspace/repos/:id — merge body into matching repo */
        async patchRepo(_req, res, params, body) {
            // Validate sync if present
            if (body.sync != null) {
                const syncErr = validateSyncStructure(body.sync);
                if (syncErr) {
                    sendJson(res, { error: syncErr }, 400);
                    return;
                }
            }
            const config = await configManager.read();
            if (!config.workspace) {
                sendJson(res, { error: 'No workspace configured' }, 404);
                return;
            }
            const idx = config.workspace.repos.findIndex(r => r.id === params.id);
            if (idx === -1) {
                sendJson(res, { error: `Repo '${params.id}' not found` }, 404);
                return;
            }
            // Merge — don't allow changing id
            const { id: _ignoreId, ...updates } = body;
            const existing = config.workspace.repos[idx];
            const merged = {
                ...existing,
                ...updates,
            };
            // Deep-merge sync if both exist
            if (updates.sync && existing.sync) {
                merged.sync = { ...existing.sync, ...updates.sync };
            }
            config.workspace.repos[idx] = merged;
            await configManager.write(config);
            sendJson(res, merged);
        },
        /** POST /api/workspace/repos — append new repo */
        async postRepo(_req, res, _params, body) {
            const { id, path, prefix } = body;
            // Validate required fields
            if (!id || !path || !prefix) {
                sendJson(res, { error: 'Fields id, path, and prefix are required' }, 400);
                return;
            }
            // Validate sync if present
            if (body.sync != null) {
                const syncErr = validateSyncStructure(body.sync);
                if (syncErr) {
                    sendJson(res, { error: syncErr }, 400);
                    return;
                }
            }
            const config = await configManager.read();
            if (!config.workspace) {
                config.workspace = { name: '', repos: [] };
            }
            // Check duplicate
            if (config.workspace.repos.some(r => r.id === id)) {
                sendJson(res, { error: `Repo '${id}' already exists` }, 400);
                return;
            }
            const newRepo = {
                id,
                path,
                prefix,
                ...(body.name ? { name: body.name } : {}),
                ...(body.techStack ? { techStack: body.techStack } : {}),
                ...(body.role ? { role: body.role } : {}),
                ...(body.sync ? { sync: body.sync } : {}),
            };
            config.workspace.repos.push(newRepo);
            await configManager.write(config);
            sendJson(res, newRepo, 201);
        },
        /** DELETE /api/workspace/repos/:id — remove repo */
        async deleteRepo(_req, res, params) {
            const config = await configManager.read();
            if (!config.workspace) {
                sendJson(res, { error: `Repo '${params.id}' not found` }, 404);
                return;
            }
            const idx = config.workspace.repos.findIndex(r => r.id === params.id);
            if (idx === -1) {
                sendJson(res, { error: `Repo '${params.id}' not found` }, 404);
                return;
            }
            config.workspace.repos.splice(idx, 1);
            await configManager.write(config);
            res.writeHead(204);
            res.end();
        },
    };
}
//# sourceMappingURL=workspace-routes.js.map