// Task Team — AI Chat Agent — 2026-03-29
const Anthropic = require('@anthropic-ai/sdk');

async function chatRoutes(app) {
    const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

    app.post('/chat', async (req, reply) => {
        const { message, context } = req.body;
        
        if (!message) {
            reply.code(400);
            return { error: 'Message is required' };
        }

        // Get user's tasks and groups for context
        const { rows: tasks } = await app.db.query(
            'SELECT title, status, priority, scheduled_at FROM tasks ORDER BY created_at DESC LIMIT 20'
        );
        const { rows: groups } = await app.db.query('SELECT name, color FROM task_groups ORDER BY order_index');
        
        const systemPrompt = `Jsi Task Team AI asistent. Pomáháš uživateli s organizací úkolů, plánováním a cíli.
Aktuální úkoly: ${JSON.stringify(tasks.map(t => ({title: t.title, status: t.status, priority: t.priority})))}
Skupiny: ${groups.map(g => g.name).join(', ')}
Odpovídej v češtině, stručně a prakticky. Pokud uživatel chce vytvořit úkol, navrhni strukturu.`;

        const response = await anthropic.messages.create({
            model: 'claude-sonnet-4-20250514',
            max_tokens: 1024,
            system: systemPrompt,
            messages: [{ role: 'user', content: message }]
        });

        return { data: { reply: response.content[0].text, model: response.model } };
    });
}
module.exports = chatRoutes;
