const sqliteClient = require('../../../common/services/sqliteClient');

function addTranscript({ sessionId, speaker, text }) {
    const db = sqliteClient.getDb();
    return new Promise((resolve, reject) => {
        const transcriptId = require('crypto').randomUUID();
        const now = Math.floor(Date.now() / 1000);
        const query = `INSERT INTO transcripts (id, session_id, start_at, speaker, text, created_at) VALUES (?, ?, ?, ?, ?, ?)`;
        db.run(query, [transcriptId, sessionId, now, speaker, text, now], function(err) {
            if (err) reject(err);
            else resolve({ id: transcriptId });
        });
    });
}

function saveSummary({ sessionId, tldr, text, bullet_json, action_json, model = 'gpt-4.1' }) {
    const db = sqliteClient.getDb();
    return new Promise((resolve, reject) => {
        const now = Math.floor(Date.now() / 1000);
        const query = `
            INSERT INTO summaries (session_id, generated_at, model, text, tldr, bullet_json, action_json, updated_at) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
            ON CONFLICT(session_id) DO UPDATE SET
                generated_at=excluded.generated_at,
                model=excluded.model,
                text=excluded.text,
                tldr=excluded.tldr,
                bullet_json=excluded.bullet_json,
                action_json=excluded.action_json,
                updated_at=excluded.updated_at
        `;
        db.run(query, [sessionId, now, model, text, tldr, bullet_json, action_json, now], function(err) {
            if (err) reject(err);
            else resolve({ changes: this.changes });
        });
    });
}

function getAllTranscriptsBySessionId(sessionId) {
    const db = sqliteClient.getDb();
    return new Promise((resolve, reject) => {
        const query = "SELECT * FROM transcripts WHERE session_id = ? ORDER BY start_at ASC";
        db.all(query, [sessionId], (err, rows) => {
            if (err) reject(err);
            else resolve(rows);
        });
    });
}

function getSummaryBySessionId(sessionId) {
    const db = sqliteClient.getDb();
    return new Promise((resolve, reject) => {
        const query = "SELECT * FROM summaries WHERE session_id = ?";
        db.get(query, [sessionId], (err, row) => {
            if (err) reject(err);
            else resolve(row || null);
        });
    });
}

module.exports = {
    addTranscript,
    saveSummary,
    getAllTranscriptsBySessionId,
    getSummaryBySessionId
}; 