"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionRepository = void 0;
const BaseRepository_js_1 = require("./BaseRepository.js");
class SessionRepository extends BaseRepository_js_1.BaseRepository {
    create(input) {
        const id = this.generateId();
        const timestamp = this.getCurrentTimestamp();
        const stmt = this.db.prepare(`
      INSERT INTO sessions (id, name, description, branch, working_directory, parent_id, default_channel, created_at, updated_at)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
    `);
        stmt.run(id, input.name || `Session ${timestamp}`, input.description || '', input.branch || null, input.working_directory || null, input.parent_id || null, input.defaultChannel || 'general', timestamp, timestamp);
        return this.getById(id);
    }
    getById(id) {
        const stmt = this.db.prepare('SELECT * FROM sessions WHERE id = ?');
        return stmt.get(id);
    }
    getAll(limit = 50) {
        const stmt = this.db.prepare('SELECT * FROM sessions ORDER BY created_at DESC LIMIT ?');
        return stmt.all(limit);
    }
    getLatest() {
        const stmt = this.db.prepare('SELECT * FROM sessions ORDER BY created_at DESC LIMIT 1');
        return stmt.get();
    }
    update(id, updates) {
        const setClause = Object.keys(updates)
            .filter(key => key !== 'id' && key !== 'created_at')
            .map(key => `${key} = ?`)
            .join(', ');
        if (setClause) {
            const values = Object.keys(updates)
                .filter(key => key !== 'id' && key !== 'created_at')
                .map(key => updates[key]);
            const stmt = this.db.prepare(`
        UPDATE sessions 
        SET ${setClause}, updated_at = CURRENT_TIMESTAMP 
        WHERE id = ?
      `);
            stmt.run(...values, id);
        }
    }
    delete(id) {
        const stmt = this.db.prepare('DELETE FROM sessions WHERE id = ?');
        stmt.run(id);
    }
    findByBranch(branch) {
        const stmt = this.db.prepare('SELECT * FROM sessions WHERE branch = ? ORDER BY created_at DESC');
        return stmt.all(branch);
    }
    findByWorkingDirectory(workingDirectory) {
        const stmt = this.db.prepare('SELECT * FROM sessions WHERE working_directory = ? ORDER BY created_at DESC');
        return stmt.all(workingDirectory);
    }
    getChildren(parentId) {
        const stmt = this.db.prepare('SELECT * FROM sessions WHERE parent_id = ? ORDER BY created_at DESC');
        return stmt.all(parentId);
    }
    getRecent(limit = 10) {
        const stmt = this.db.prepare('SELECT * FROM sessions ORDER BY created_at DESC LIMIT ?');
        return stmt.all(limit);
    }
}
exports.SessionRepository = SessionRepository;
