"use strict";
/**
 * Memo database operations
 * CRUD operations for worktree_memos table
 *
 * Issue #479: Extracted from db.ts for single-responsibility separation
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMemosByWorktreeId = getMemosByWorktreeId;
exports.getMemoById = getMemoById;
exports.createMemo = createMemo;
exports.updateMemo = updateMemo;
exports.deleteMemo = deleteMemo;
exports.reorderMemos = reorderMemos;
const crypto_1 = require("crypto");
/**
 * Map database row to WorktreeMemo model
 */
function mapMemoRow(row) {
    return {
        id: row.id,
        worktreeId: row.worktree_id,
        title: row.title,
        content: row.content,
        position: row.position,
        createdAt: new Date(row.created_at),
        updatedAt: new Date(row.updated_at),
    };
}
/**
 * Get all memos for a worktree, sorted by position
 */
function getMemosByWorktreeId(db, worktreeId) {
    const stmt = db.prepare(`
    SELECT id, worktree_id, title, content, position, created_at, updated_at
    FROM worktree_memos
    WHERE worktree_id = ?
    ORDER BY position ASC
  `);
    const rows = stmt.all(worktreeId);
    return rows.map(mapMemoRow);
}
/**
 * Get a memo by ID
 *
 * @param db - Database instance
 * @param memoId - ID of the memo
 * @returns Memo or null if not found
 */
function getMemoById(db, memoId) {
    const stmt = db.prepare(`
    SELECT id, worktree_id, title, content, position, created_at, updated_at
    FROM worktree_memos
    WHERE id = ?
  `);
    const row = stmt.get(memoId);
    return row ? mapMemoRow(row) : null;
}
/**
 * Create a new memo for a worktree
 *
 * @param db - Database instance
 * @param worktreeId - ID of the worktree
 * @param options - Memo options (title, content, position)
 * @returns Created memo
 */
function createMemo(db, worktreeId, options) {
    const id = (0, crypto_1.randomUUID)();
    const now = Date.now();
    const title = options.title ?? 'Memo';
    const content = options.content ?? '';
    const stmt = db.prepare(`
    INSERT INTO worktree_memos (id, worktree_id, title, content, position, created_at, updated_at)
    VALUES (?, ?, ?, ?, ?, ?, ?)
  `);
    stmt.run(id, worktreeId, title, content, options.position, now, now);
    return {
        id,
        worktreeId,
        title,
        content,
        position: options.position,
        createdAt: new Date(now),
        updatedAt: new Date(now),
    };
}
/**
 * Update an existing memo
 *
 * @param db - Database instance
 * @param memoId - ID of the memo to update
 * @param updates - Fields to update (title and/or content)
 */
function updateMemo(db, memoId, updates) {
    const now = Date.now();
    const assignments = ['updated_at = ?'];
    const params = [now];
    if (updates.title !== undefined) {
        assignments.push('title = ?');
        params.push(updates.title);
    }
    if (updates.content !== undefined) {
        assignments.push('content = ?');
        params.push(updates.content);
    }
    params.push(memoId);
    const stmt = db.prepare(`
    UPDATE worktree_memos
    SET ${assignments.join(', ')}
    WHERE id = ?
  `);
    stmt.run(...params);
}
/**
 * Delete a memo by ID
 *
 * @param db - Database instance
 * @param memoId - ID of the memo to delete
 */
function deleteMemo(db, memoId) {
    const stmt = db.prepare(`
    DELETE FROM worktree_memos
    WHERE id = ?
  `);
    stmt.run(memoId);
}
/**
 * Reorder memos for a worktree
 *
 * Uses a two-step approach to handle UNIQUE constraint on (worktree_id, position):
 * 1. Set all positions to negative values (temporary)
 * 2. Set new positions from the provided order
 *
 * @param db - Database instance
 * @param worktreeId - ID of the worktree
 * @param memoIds - Array of memo IDs in the desired order
 */
function reorderMemos(db, worktreeId, memoIds) {
    if (memoIds.length === 0) {
        return;
    }
    const now = Date.now();
    db.transaction(() => {
        // Step 1: Set all positions to negative values (to avoid UNIQUE constraint violations)
        const resetStmt = db.prepare(`
      UPDATE worktree_memos
      SET position = -1 - position
      WHERE id = ?
    `);
        for (const memoId of memoIds) {
            resetStmt.run(memoId);
        }
        // Step 2: Set new positions based on array order
        const updateStmt = db.prepare(`
      UPDATE worktree_memos
      SET position = ?, updated_at = ?
      WHERE id = ?
    `);
        memoIds.forEach((memoId, index) => {
            updateStmt.run(index, now, memoId);
        });
    })();
}
