/**
 * Interactive Session Database Mapper
 *
 * Maps between InteractiveSession domain objects and SQLite database rows.
 *
 * Mapping Rules:
 * - TypeScript objects (camelCase) <-> SQL columns (snake_case)
 * - Dates stored as INTEGER (unix milliseconds)
 * - Optional stoppedAt stored as NULL when absent
 * - InteractiveSessionStatus stored as string enum values
 */
/**
 * Maps InteractiveSession domain object to database row.
 * Converts Date objects to unix milliseconds for SQL storage.
 */
export function toDatabase(session) {
    const full = session;
    return {
        id: session.id,
        feature_id: session.featureId,
        status: session.status,
        agent_session_id: full.agentSessionId ?? null,
        turn_status: full.turnStatus ?? 'idle',
        started_at: session.startedAt instanceof Date ? session.startedAt.getTime() : session.startedAt,
        stopped_at: session.stoppedAt instanceof Date ? session.stoppedAt.getTime() : (session.stoppedAt ?? null),
        last_activity_at: session.lastActivityAt instanceof Date
            ? session.lastActivityAt.getTime()
            : session.lastActivityAt,
        created_at: session.createdAt instanceof Date ? session.createdAt.getTime() : session.createdAt,
        updated_at: session.updatedAt instanceof Date ? session.updatedAt.getTime() : session.updatedAt,
        total_cost_usd: full.totalCostUsd ?? 0,
        total_input_tokens: full.totalInputTokens ?? 0,
        total_output_tokens: full.totalOutputTokens ?? 0,
        total_turns: full.totalTurns ?? 0,
    };
}
/**
 * Maps database row to InteractiveSessionFull (InteractiveSession + extra DB columns).
 * Converts unix milliseconds back to Date objects.
 */
export function fromDatabase(row) {
    return {
        id: row.id,
        featureId: row.feature_id,
        status: row.status,
        turnStatus: row.turn_status,
        agentSessionId: row.agent_session_id,
        startedAt: new Date(row.started_at),
        lastActivityAt: new Date(row.last_activity_at),
        createdAt: new Date(row.created_at),
        updatedAt: new Date(row.updated_at),
        totalCostUsd: row.total_cost_usd ?? 0,
        totalInputTokens: row.total_input_tokens ?? 0,
        totalOutputTokens: row.total_output_tokens ?? 0,
        totalTurns: row.total_turns ?? 0,
        ...(row.stopped_at !== null && { stoppedAt: new Date(row.stopped_at) }),
    };
}
