import { Router } from 'express';
import { getDb } from '../db';
import { execSync } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PROJECT_ROOT = path.resolve(__dirname, '..', '..', '..');
const PIPELINE_SCRIPT = path.join(PROJECT_ROOT, '.claude', 'skills', 'video-from-script', 'scripts', 'pipeline.js');

export const pipelineRouter = Router();

pipelineRouter.get('/conversations', (req, res) => {
  const { search } = req.query;
  let sql = 'SELECT * FROM conversations';
  const params: string[] = [];
  if (search) {
    sql += ' WHERE title LIKE ?';
    params.push(`%${search}%`);
  }
  sql += ' ORDER BY pinned DESC, updated_at DESC LIMIT 100';
  const rows = getDb().prepare(sql).all(...params);
  res.json(rows);
});

pipelineRouter.get('/conversations/:id/messages', (req, res) => {
  const rows = getDb().prepare(
    'SELECT * FROM messages WHERE conversation_id = ? ORDER BY created_at'
  ).all(req.params.id);
  res.json(rows);
});

pipelineRouter.delete('/conversations/:id', (req, res) => {
  getDb().prepare('DELETE FROM messages WHERE conversation_id = ?').run(req.params.id);
  getDb().prepare('DELETE FROM conversations WHERE id = ?').run(req.params.id);
  res.json({ ok: true });
});

// Delete a single message
pipelineRouter.delete('/messages/:id', (req, res) => {
  getDb().prepare('DELETE FROM messages WHERE id = ?').run(req.params.id);
  res.json({ ok: true });
});

// Update conversation (rename / toggle pin)
pipelineRouter.patch('/conversations/:id', (req, res) => {
  const { title, pinned } = req.body;
  if (title !== undefined) {
    getDb().prepare(
      'UPDATE conversations SET title = ?, updated_at = datetime(\'now\') WHERE id = ?'
    ).run(title, req.params.id);
  }
  if (pinned !== undefined) {
    getDb().prepare(
      'UPDATE conversations SET pinned = ?, updated_at = datetime(\'now\') WHERE id = ?'
    ).run(pinned ? 1 : 0, req.params.id);
  }
  res.json({ ok: true });
});

pipelineRouter.get('/status', (req, res) => {
  const { manifest } = req.query;
  if (!manifest) return res.status(400).json({ error: 'manifest path required' });
  try {
    const result = execSync(`node "${PIPELINE_SCRIPT}" status --manifest "${manifest}"`, {
      cwd: PROJECT_ROOT, encoding: 'utf-8',
    });
    res.json({ output: result });
  } catch (e) {
    res.status(500).json({ error: (e as Error).message });
  }
});

pipelineRouter.post('/resume', (req, res) => {
  const { manifest } = req.body;
  if (!manifest) return res.status(400).json({ error: 'manifest path required' });
  try {
    const result = execSync(`node "${PIPELINE_SCRIPT}" run --manifest "${manifest}" --resume`, {
      cwd: PROJECT_ROOT, encoding: 'utf-8',
    });
    res.json({ output: result });
  } catch (e) {
    res.status(500).json({ error: (e as Error).message });
  }
});
