import { Router } from 'express';
import { z } from 'zod';
import * as store from '../storage/sqlite-store.js';
import { validateIdentifier } from '../storage/sql-safety.js';
const router = Router();
const createSchema = z.object({
    name: z.string().min(1),
    description: z.string().optional(),
    workflowIds: z.array(z.string()).optional(),
});
const updateSchema = z.object({
    name: z.string().min(1).optional(),
    description: z.string().optional(),
    workflowIds: z.array(z.string()).optional(),
});
const sqlSchema = z.object({
    sql: z.string().min(1),
    params: z.union([z.array(z.any()), z.record(z.string(), z.any())]).optional(),
});
// GET /api/sqlite/databases?workflowId=
router.get('/databases', (req, res) => {
    res.json(store.listDatabases(typeof req.query.workflowId === 'string' ? req.query.workflowId : undefined));
});
// POST /api/sqlite/databases
router.post('/databases', (req, res) => {
    const parsed = createSchema.safeParse(req.body);
    if (!parsed.success) {
        res.status(400).json({ error: parsed.error.message });
        return;
    }
    try {
        res.json(store.createDatabase(parsed.data));
    }
    catch (e) {
        res.status(400).json({ error: e.message });
    }
});
// PATCH /api/sqlite/databases/:id
router.patch('/databases/:id', (req, res) => {
    const parsed = updateSchema.safeParse(req.body);
    if (!parsed.success) {
        res.status(400).json({ error: parsed.error.message });
        return;
    }
    const meta = store.updateDatabase(req.params.id, parsed.data);
    if (!meta) {
        res.status(404).json({ error: 'Database not found' });
        return;
    }
    res.json(meta);
});
// DELETE /api/sqlite/databases/:id
router.delete('/databases/:id', (req, res) => {
    if (!store.deleteDatabase(req.params.id)) {
        res.status(404).json({ error: 'Database not found' });
        return;
    }
    res.json({ ok: true });
});
// GET /api/sqlite/databases/:id/tables
router.get('/databases/:id/tables', (req, res) => {
    try {
        res.json(store.listTables(req.params.id));
    }
    catch (e) {
        res.status(500).json({ error: e.message });
    }
});
// GET /api/sqlite/databases/:id/tables/:table/columns
router.get('/databases/:id/tables/:table/columns', (req, res) => {
    try {
        validateIdentifier(req.params.table, 'table');
        res.json(store.describeTable(req.params.id, req.params.table));
    }
    catch (e) {
        res.status(400).json({ error: e.message });
    }
});
// POST /api/sqlite/databases/:id/query
router.post('/databases/:id/query', (req, res) => {
    const parsed = sqlSchema.safeParse(req.body);
    if (!parsed.success) {
        res.status(400).json({ error: parsed.error.message });
        return;
    }
    try {
        res.json(store.query(req.params.id, parsed.data.sql, parsed.data.params));
    }
    catch (e) {
        res.status(400).json({ error: e.message });
    }
});
// POST /api/sqlite/databases/:id/exec
router.post('/databases/:id/exec', (req, res) => {
    const parsed = sqlSchema.safeParse(req.body);
    if (!parsed.success) {
        res.status(400).json({ error: parsed.error.message });
        return;
    }
    try {
        res.json(store.exec(req.params.id, parsed.data.sql, parsed.data.params));
    }
    catch (e) {
        res.status(400).json({ error: e.message });
    }
});
export default router;
//# sourceMappingURL=sqlite.js.map