import express from 'express';
import Database from 'better-sqlite3';
import { v4 as uuidv4 } from 'uuid';
import puppeteer from 'puppeteer';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEY = process.env.API_KEY || 'crystal-secret-key';
// Initialize database
const dbPath = process.env.DB_PATH || '/data/crystal.db';
const db = new Database(dbPath);
// Create tables
db.exec(`
CREATE TABLE IF NOT EXISTS reports (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
report_type TEXT NOT NULL,
html_content TEXT NOT NULL,
summary TEXT,
created_at TEXT DEFAULT (datetime('now')),
period_start TEXT,
period_end TEXT
)
`);
app.use(express.json({ limit: '10mb' }));
app.use(express.static(path.join(__dirname, '../public')));
// Helper: Wrap content in layout
function renderPage(title, content, extra = '') {
return `
${title} - Phil Dashboard
${extra}
📋 Phil Dashboard
${content}
`;
}
// Dashboard - List all reports
app.get('/', (req, res) => {
const reports = db.prepare(`
SELECT id, title, report_type, summary, created_at, period_start, period_end
FROM reports ORDER BY created_at DESC
`).all();
const reportsList = reports.length === 0
? '