"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckpointRepository = void 0;
const BaseRepository_js_1 = require("./BaseRepository.js");
class CheckpointRepository extends BaseRepository_js_1.BaseRepository {
    create(sessionId, input) {
        const id = this.generateId();
        const timestamp = this.getCurrentTimestamp();
        const stmt = this.db.prepare(`
      INSERT INTO checkpoints (id, session_id, name, description, git_status, git_branch, created_at)
      VALUES (?, ?, ?, ?, ?, ?, ?)
    `);
        stmt.run(id, sessionId, input.name, input.description || null, input.git_status || null, input.git_branch || null, timestamp);
        return this.getById(id);
    }
    getById(id) {
        const stmt = this.db.prepare('SELECT * FROM checkpoints WHERE id = ?');
        return stmt.get(id);
    }
    getByName(sessionId, name) {
        const stmt = this.db.prepare(`
      SELECT * FROM checkpoints 
      WHERE session_id = ? AND name = ?
    `);
        return stmt.get(sessionId, name);
    }
    getBySessionId(sessionId) {
        const stmt = this.db.prepare(`
      SELECT * FROM checkpoints 
      WHERE session_id = ? 
      ORDER BY created_at DESC
    `);
        return stmt.all(sessionId);
    }
    delete(id) {
        // Delete related checkpoint items and files first
        this.db.prepare('DELETE FROM checkpoint_items WHERE checkpoint_id = ?').run(id);
        this.db.prepare('DELETE FROM checkpoint_files WHERE checkpoint_id = ?').run(id);
        // Delete the checkpoint
        this.db.prepare('DELETE FROM checkpoints WHERE id = ?').run(id);
    }
    deleteBySessionId(sessionId) {
        const checkpoints = this.getBySessionId(sessionId);
        for (const checkpoint of checkpoints) {
            this.delete(checkpoint.id);
        }
    }
    // Checkpoint Items Management
    addContextItem(checkpointId, contextItemId) {
        const stmt = this.db.prepare(`
      INSERT INTO checkpoint_items (id, checkpoint_id, context_item_id)
      VALUES (?, ?, ?)
    `);
        stmt.run(this.generateId(), checkpointId, contextItemId);
    }
    addContextItems(checkpointId, contextItemIds) {
        const stmt = this.db.prepare(`
      INSERT INTO checkpoint_items (id, checkpoint_id, context_item_id)
      VALUES (?, ?, ?)
    `);
        for (const itemId of contextItemIds) {
            stmt.run(this.generateId(), checkpointId, itemId);
        }
    }
    getContextItemIds(checkpointId) {
        const stmt = this.db.prepare(`
      SELECT context_item_id 
      FROM checkpoint_items 
      WHERE checkpoint_id = ?
    `);
        return stmt.all(checkpointId).map((row) => row.context_item_id);
    }
    // Checkpoint Files Management
    addFile(checkpointId, fileCacheId) {
        const stmt = this.db.prepare(`
      INSERT INTO checkpoint_files (id, checkpoint_id, file_cache_id)
      VALUES (?, ?, ?)
    `);
        stmt.run(this.generateId(), checkpointId, fileCacheId);
    }
    addFiles(checkpointId, fileCacheIds) {
        const stmt = this.db.prepare(`
      INSERT INTO checkpoint_files (id, checkpoint_id, file_cache_id)
      VALUES (?, ?, ?)
    `);
        for (const fileId of fileCacheIds) {
            stmt.run(this.generateId(), checkpointId, fileId);
        }
    }
    getFileCacheIds(checkpointId) {
        const stmt = this.db.prepare(`
      SELECT file_cache_id 
      FROM checkpoint_files 
      WHERE checkpoint_id = ?
    `);
        return stmt.all(checkpointId).map((row) => row.file_cache_id);
    }
    // Complete checkpoint with items and files
    createComplete(sessionId, input, contextItemIds = [], fileCacheIds = []) {
        const checkpoint = this.create(sessionId, input);
        if (contextItemIds.length > 0) {
            this.addContextItems(checkpoint.id, contextItemIds);
        }
        if (fileCacheIds.length > 0) {
            this.addFiles(checkpoint.id, fileCacheIds);
        }
        return checkpoint;
    }
    getStatsBySession(sessionId) {
        const checkpointCount = this.db
            .prepare(`
      SELECT COUNT(*) as count 
      FROM checkpoints 
      WHERE session_id = ?
    `)
            .get(sessionId);
        const itemCount = this.db
            .prepare(`
      SELECT COUNT(*) as count
      FROM checkpoint_items ci
      JOIN checkpoints c ON ci.checkpoint_id = c.id
      WHERE c.session_id = ?
    `)
            .get(sessionId);
        const fileCount = this.db
            .prepare(`
      SELECT COUNT(*) as count
      FROM checkpoint_files cf
      JOIN checkpoints c ON cf.checkpoint_id = c.id
      WHERE c.session_id = ?
    `)
            .get(sessionId);
        return {
            count: checkpointCount.count || 0,
            totalItems: itemCount.count || 0,
            totalFiles: fileCount.count || 0,
        };
    }
}
exports.CheckpointRepository = CheckpointRepository;
