export async function schedulerRoutes(app, opts) {
    const { db, schedulerService } = opts;
    // GET /api/scheduler — list all scheduled jobs
    app.get('/api/scheduler', {
        preHandler: [app.requireAuth],
    }, async (_request, reply) => {
        const rows = db.prepare('SELECT * FROM scheduled_jobs ORDER BY squad_name').all();
        return reply.send({ data: rows });
    });
    // GET /api/scheduler/:squad — job details
    app.get('/api/scheduler/:squad', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        const row = db.prepare('SELECT * FROM scheduled_jobs WHERE squad_name = ?')
            .get(request.params.squad);
        if (!row)
            return reply.code(404).send({ error: 'Scheduled job not found' });
        return reply.send({ data: row });
    });
    // POST /api/scheduler — create or update a scheduled job
    app.post('/api/scheduler', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        const { squad_name, cron_expression, prompt, timezone, timeout_minutes, retry_on_failure } = request.body;
        if (!squad_name || typeof squad_name !== 'string' || !squad_name.trim()) {
            return reply.code(400).send({ error: 'squad_name is required' });
        }
        if (!cron_expression || typeof cron_expression !== 'string' || !cron_expression.trim()) {
            return reply.code(400).send({ error: 'cron_expression is required' });
        }
        if (!prompt || typeof prompt !== 'string' || !prompt.trim()) {
            return reply.code(400).send({ error: 'prompt is required' });
        }
        if (schedulerService) {
            const ok = schedulerService.registerDynamic(squad_name.trim(), {
                cron: cron_expression.trim(),
                prompt: prompt.trim(),
                timezone: timezone?.trim() || undefined,
                timeout_minutes: timeout_minutes ?? 30,
                retry_on_failure: retry_on_failure ?? 0,
            });
            if (!ok) {
                return reply.code(400).send({ error: 'Invalid cron expression' });
            }
        }
        else {
            // No scheduler service — just store in DB
            db.prepare(`
        INSERT INTO scheduled_jobs (squad_name, cron_expression, prompt, mode, timeout_minutes, retry_on_failure, timezone, status)
        VALUES (?, ?, ?, 'autonomous', ?, ?, ?, 'active')
        ON CONFLICT(squad_name) DO UPDATE SET
          cron_expression = excluded.cron_expression,
          prompt = excluded.prompt,
          timeout_minutes = excluded.timeout_minutes,
          retry_on_failure = excluded.retry_on_failure,
          timezone = excluded.timezone,
          status = 'active',
          updated_at = datetime('now')
      `).run(squad_name.trim(), cron_expression.trim(), prompt.trim(), timeout_minutes ?? 30, retry_on_failure ?? 0, timezone?.trim() || null);
        }
        const row = db.prepare('SELECT * FROM scheduled_jobs WHERE squad_name = ?').get(squad_name.trim());
        return reply.code(201).send({ data: row });
    });
    // POST /api/scheduler/:squad/pause
    app.post('/api/scheduler/:squad/pause', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        if (schedulerService)
            schedulerService.pause(request.params.squad);
        return reply.send({ ok: true });
    });
    // POST /api/scheduler/:squad/resume
    app.post('/api/scheduler/:squad/resume', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        if (schedulerService)
            schedulerService.resume(request.params.squad);
        return reply.send({ ok: true });
    });
    // POST /api/scheduler/:squad/trigger
    app.post('/api/scheduler/:squad/trigger', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        if (schedulerService)
            schedulerService.triggerNow(request.params.squad);
        return reply.send({ ok: true, message: 'Triggered' });
    });
    // DELETE /api/scheduler/:squad
    app.delete('/api/scheduler/:squad', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        if (schedulerService)
            schedulerService.unregister(request.params.squad);
        return reply.send({ ok: true });
    });
    // GET /api/scheduler/:squad/history
    app.get('/api/scheduler/:squad/history', {
        preHandler: [app.requireAuth],
    }, async (request, reply) => {
        const limit = Math.min(parseInt(request.query.limit ?? '20', 10) || 20, 100);
        const rows = db.prepare("SELECT * FROM squad_activities WHERE squad_code = ? AND triggered_by = 'schedule' ORDER BY started_at DESC LIMIT ?").all(request.params.squad, limit);
        return reply.send({ data: rows });
    });
}
//# sourceMappingURL=scheduler-routes.js.map