/**
 * Views API routes
 *
 * Per-user saved views backed by AuthService SQLite.
 * Views are scoped to the authenticated user (or user_id=0 in no-auth mode).
 */
export async function registerViewsApiRoutes(fastify, authService, workspaceManager) {
    function getUserId(req) {
        return req.user?.id ?? 0;
    }
    function getEntityTypeCounts() {
        const counts = {};
        try {
            for (const repo of workspaceManager.getAllRepoConfigs()) {
                const graph = workspaceManager.getGraph(repo.name);
                const entities = graph.search({});
                for (const e of entities) {
                    const type = e.data?.entity_type;
                    if (type)
                        counts[type] = (counts[type] ?? 0) + 1;
                }
            }
        }
        catch { /* ignore */ }
        return counts;
    }
    // GET /api/views — list user's views (auto-creates defaults on first access)
    fastify.get('/api/views', async (req, reply) => {
        const userId = getUserId(req);
        authService.ensureDefaultViews(userId, getEntityTypeCounts());
        return reply.send(authService.getUserViews(userId));
    });
    // POST /api/views — create a new view
    fastify.post('/api/views', async (req, reply) => {
        const userId = getUserId(req);
        const user = req.user;
        const { name, viewType, config } = req.body ?? {};
        if (!name) {
            return reply.status(400).send({ error: 'name is required' });
        }
        const view = authService.createUserView(userId, name, viewType ?? 'list', config ?? {}, user?.displayName ?? user?.email);
        return reply.send(view);
    });
    // POST /api/views/:id/share — toggle view visibility
    fastify.post('/api/views/:id/share', async (req, reply) => {
        const userId = getUserId(req);
        const viewId = Number(req.params.id);
        const existing = authService.getUserView(viewId, userId);
        if (!existing) {
            return reply.status(404).send({ error: 'View not found' });
        }
        const visibility = req.body?.visibility ?? 'private';
        authService.setViewVisibility(viewId, userId, visibility);
        return reply.send({ ok: true, visibility });
    });
    // PUT /api/views/:id — update a view
    fastify.put('/api/views/:id', async (req, reply) => {
        const userId = getUserId(req);
        const viewId = Number(req.params.id);
        const existing = authService.getUserView(viewId, userId);
        if (!existing) {
            return reply.status(404).send({ error: 'View not found' });
        }
        authService.updateUserView(viewId, userId, {
            name: req.body?.name,
            config: req.body?.config,
            sortOrder: req.body?.sortOrder,
        });
        return reply.send({ ok: true });
    });
    // DELETE /api/views/:id — delete a view
    fastify.delete('/api/views/:id', async (req, reply) => {
        const userId = getUserId(req);
        const viewId = Number(req.params.id);
        const existing = authService.getUserView(viewId, userId);
        if (!existing) {
            return reply.status(404).send({ error: 'View not found' });
        }
        authService.deleteUserView(viewId, userId);
        return reply.send({ ok: true });
    });
    // ── Workspace views (folder-views redesign) ──
    // Workspace-shared, scope-keyed view sets. Scope is `folder:<id>` |
    // `type:<entityType>` | `recent`. Not per-user — everyone sees the same set.
    const VIEW_TYPES = new Set(['list', 'gallery', 'kanban', 'timeline', 'calendar', 'graph']);
    // GET /api/workspace-views?scope=… — ordered views for a scope (seeds defaults
    // on first access: a List, plus Calendar+Graph for `recent`).
    fastify.get('/api/workspace-views', async (req, reply) => {
        const scope = req.query?.scope;
        if (!scope)
            return reply.status(400).send({ error: 'scope is required' });
        return reply.send(authService.ensureViewsForScope(scope));
    });
    // POST /api/workspace-views — add a view to a scope. One-of-each is enforced
    // by the DB; `recent` is a fixed set and rejects additions.
    fastify.post('/api/workspace-views', async (req, reply) => {
        const user = req.user;
        const { scope, viewType, config } = req.body ?? {};
        if (!scope)
            return reply.status(400).send({ error: 'scope is required' });
        if (!viewType || !VIEW_TYPES.has(viewType)) {
            return reply.status(400).send({ error: 'valid viewType is required' });
        }
        if (scope === 'recent') {
            return reply.status(400).send({ error: 'The Recent view set is fixed' });
        }
        const view = authService.createWorkspaceView(scope, viewType, config ?? {}, user?.displayName ?? user?.email);
        return reply.send(view);
    });
    // PUT /api/workspace-views/:id — update config / position / name.
    fastify.put('/api/workspace-views/:id', async (req, reply) => {
        const viewId = Number(req.params.id);
        const existing = authService.getWorkspaceView(viewId);
        if (!existing)
            return reply.status(404).send({ error: 'View not found' });
        authService.updateWorkspaceView(viewId, {
            config: req.body?.config,
            position: req.body?.position,
            name: req.body?.name,
        });
        return reply.send({ ok: true });
    });
    // DELETE /api/workspace-views/:id — remove a view. List is un-removable (every
    // scope keeps ≥1 view) and the `recent` set is fixed.
    fastify.delete('/api/workspace-views/:id', async (req, reply) => {
        const viewId = Number(req.params.id);
        const existing = authService.getWorkspaceView(viewId);
        if (!existing)
            return reply.status(404).send({ error: 'View not found' });
        if (existing.viewType === 'list') {
            return reply.status(400).send({ error: 'The List view cannot be removed' });
        }
        if (existing.scope === 'recent') {
            return reply.status(400).send({ error: 'The Recent view set is fixed' });
        }
        authService.deleteWorkspaceView(viewId);
        return reply.send({ ok: true });
    });
}
//# sourceMappingURL=views-api.js.map