/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Hono } from 'hono';
import { createTask, listTasks, claimTask, completeTask, cancelTask, blockTask, archiveTask, deleteTask, } from '../../services/task-state.js';
import { getTaskFiles, getTaskTimeline } from '../../services/task-files.js';
import { getWorkspaceCwd } from '../middleware/workspace.js';
export function tasksRoutes(cwd) {
    const app = new Hono();
    app.get('/', (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const status = c.req.query('status');
        const profile = c.req.query('profile');
        const limit = c.req.query('limit') ? Number(c.req.query('limit')) : undefined;
        const records = listTasks(dir, { status, profile, limit });
        return c.json({ records });
    });
    app.post('/', async (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const body = await c.req.json();
        const record = createTask(dir, body);
        return c.json({ record }, 201);
    });
    app.get('/:id/files', async (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const id = c.req.param('id');
        try {
            const files = await getTaskFiles(dir, id);
            return c.json({ files });
        }
        catch (e) {
            const msg = e.message;
            const status = msg.startsWith('Invalid task ID') ? 400 : 500;
            return c.json({ error: msg }, status);
        }
    });
    app.get('/:id/timeline', async (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const id = c.req.param('id');
        try {
            const events = await getTaskTimeline(dir, id);
            return c.json({ events });
        }
        catch (e) {
            const msg = e.message;
            const status = msg.startsWith('Invalid task ID') ? 400 : 500;
            return c.json({ error: msg }, status);
        }
    });
    app.patch('/:id', async (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const id = c.req.param('id');
        const body = await c.req.json();
        try {
            let record;
            switch (body.action) {
                case 'claim':
                    record = claimTask(dir, { id, profile: body.profile });
                    break;
                case 'complete':
                    record = completeTask(dir, { id, profile: body.profile });
                    break;
                case 'cancel':
                    record = cancelTask(dir, id, body.reason);
                    break;
                case 'block':
                    record = blockTask(dir, id, body.reason);
                    break;
                case 'archive':
                    record = archiveTask(dir, id);
                    break;
                case 'delete':
                    record = deleteTask(dir, id);
                    break;
                default:
                    return c.json({ error: `Unknown action: ${body.action}` }, 400);
            }
            return c.json({ record });
        }
        catch (e) {
            return c.json({ error: e.message }, 404);
        }
    });
    return app;
}
//# sourceMappingURL=tasks.js.map